C Programming Questions and Answers

Questions Index

Question: Define the term 'variable'. What are the rules to be followed to name a variable in "C"? Write the syntax to declare a variable and also mention how to assign values to it (initialize them).

Answer

Variable is an identifier whose value changes from time to time during execution. It is a named data storage location in your computer’s memory. By using a variable’s name in your program, you are, in effect, referring to the data stored there.

A variable represents a single data item i.e. a numeric quantity or a character constant or a string constant.

A value must be assigned to the variables at some point of time in the program which is termed as assignment statement.

The variable can then be accessed later in the program. If the variable is accessed before it is assigned a value, it may give garbage value.

All variables have three essential attributes:

variable

Declaring Variables

int a;
int num1, num2;
float amount;

Initialising Variables

The value is assigned at the declaration time. For example,

int b = 10;

The value can be assigned anywhere inside the program using = operator after it is declared.

num1 = 89;

Rules for Forming Identifiers

Identifiers are defined according to the following rules:

1. It consists of letters and digits.
2. First character must be an alphabet or underscore.
3. Both upper and lower cases are allowed. Same text of different case is not equivalent, for example: TEXT is not same as text.
4. Except the special character underscore ( _ ), no other special symbols can be used. For example,
some valid identifiers are shown below:
X
X123
_XI
temp
tax_rate