MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 3 of 11

Block 1 · Unit 2Data Types, Operators and Expressions10 min · Section 3 of 11

2.3Variables: Declaration and Initialisation

Covers IGNOU MCS-201 §2.6, 2.7, 2.8

A program needs somewhere to keep the values it works with: marks, totals, counters. These storage places are variables. This section explains how to create them, give them values, and avoid the classic bug of using a variable before it has a value.

What a variable is

The type of a variable never changes after it is declared; only its value does.

Declaring variables

A declaration tells the compiler the name and type of a variable so that memory can be set aside for it:

Syntax
data-type variable-name1, variable-name2, ... ;

Examples: int count;, float length, width;, long population;, char grade;.

Every variable must be declared before it is used. Using an undeclared name is always a compile-time error.

Initialising variables

Initialisation gives a variable its first value. There are two ways:

  1. In the declaration: int a = 10;, float rate = 0.4e-5f;, char grade = 'A';
  2. By a later assignment: int a; then a = 10;

A local variable that is declared but never given a value holds an indeterminate value, often called a garbage value, and reading it is undefined behaviour. Variables declared outside all functions (global variables) are automatically set to zero.

Key takeaways

  • A variable has a name, a type, a value and an address; only the value changes.
  • Declare before use: type name;. Undeclared names are compile-time errors.
  • Initialise in the declaration or by assignment; uninitialised locals hold garbage.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does a declaration such as float price; do?

Show answer

Answer: B. Tells the compiler the name and type, so memory can be reserved

A declaration introduces the name with its type. It does not guarantee a value; a local price holds garbage until assigned.

Question 2 · multiple choice

What happens if a program uses a variable count that was never declared?

Show answer

Answer: D. Compilation fails with an error

C never declares variables automatically, so the compiler reports an undeclared identifier. Garbage values come from declared but uninitialised variables.

Question 3 · multiple choice

Which initialisation is correct?

Show answer

Answer: C. float b = 0.4e-5f;

A floating constant must be written without spaces, using a decimal point. A comma is not a decimal separator in C.

Question 4 · multiple choice

What does this program print?

C · assign_trace.c
#include <stdio.h>

int main(void)
{
    int a = 5, b;
    b = a * 2;
    a = b + 1;
    printf("%d %d\n", a, b);
    return 0;
}
Show answer

Answer: B. 11 10

b becomes 5 × 2 = 10, then a becomes 10 + 1 = 11. The old value of a is replaced, so 5 is not printed.

Question 5 · multiple choice

Which is the best choice of type for storing a student’s percentage, such as 87.4?

Show answer

Answer: D. float

A percentage can have a fractional part, which needs a floating type. An int would drop the .4.

Question 6 · short answer

Explain the difference between declaring and initialising a variable, using int total; and int total = 0;.

Show worked answer

int total; is a declaration only: it creates an int variable called total but, inside a function, its value is indeterminate (garbage) until something is assigned. int total = 0; declares the variable and initialises it in the same statement, so it starts with the known value 0. A running total must be initialised to 0 before values are added to it.

↑ ↓ move↵ openTab switch type