Skip to study material
← Question index

QUESTION 45 · C fundamentals

Distinguish declaration from initialisation with an example.

2 marks

Answer

A declaration introduces an object and its type; initialisation supplies its starting value.

  • int count; declares count without supplying an initial value.
  • int count = 0; declares and initialises count. An automatic local object needs a valid value before it is read.

QUESTION 46 · C fundamentals

Define a variable. Explain declaration, initialisation and assignment.

5 marks

Answer in brief

A variable is a named object used to store a value. The value of an ordinary modifiable variable can change while the program runs.

Declaration, initialisation and assignment

  1. Declaration: Introduces the name and type. Example: int score;.
  2. Initialisation: Provides a starting value when the object is created. Example: int score = 70;.
  3. Assignment: Stores a new value in an existing variable. Example: score = 75;.

Example

After int score = 70;, score contains 70. If bonus is 5, the statement score = score + bonus; calculates 75 and stores it back in score. The declared type remains int. Only the stored value changes.

Declaration and definition

A declaration gives information about a name. An object definition also provides its storage. For example, a local int count; defines an object even though it does not give it a starting value. extern int shared_count; is an example of a declaration whose definition can be supplied elsewhere. These terms should not be confused with initialisation.

Important points

  1. Initialise before use: An ordinary local automatic variable without an initialiser does not have a useful guaranteed starting value. Give it a valid value before reading it.
  2. Use the correct type: A value must be suitable for the destination type and its range.
  3. Understand const: A const-qualified object cannot be changed through that object.
  4. Use clear names: A name such as total_marks makes the purpose easier to understand.

Reference table

↔ Scroll horizontally on a small screen
Declaration, initialisation and assignment
OperationExamplePurpose
Declarationint total;Introduces total as an int object.
Initialisationint total = 0;Supplies the object’s initial value.
Assignmenttotal = 12;Replaces the value of an existing modifiable object.

Key points

  • An ordinary modifiable variable can change during execution.
  • Initialisation happens as the object is created; assignment happens later.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • March/April 2023 · Q1(b) · 3 marks

    How do you declare a variable?

    Paper P02 · page 1
  • July 2021 · Q1(a) · 7 marks

    Differentiate declaration and definition of a variable, with examples.

    Paper P04 · page 1
View all matching exam references →