|What is Variable?
A variable is a named location in computer’s memory.
A variable is used to store value temporary during the execution of the program.
One of the characteristic of variable is that the value of variable may change during execution of the program.
|Rules for naming variable.
Programmer have to keep following rules in mind while defining variables:
(1) The name of variable can consist of letters (A-Z, a-z), digits (0-9) and underscore symbol (_). No other special symbols can be used in the name of variable.
(2) Variable name can not start with digit.
(3) Keywords can not be used as a name of variable.
(4) In the name of variable uppercase and lowercase letters are considered as different.
(5) White spaces can not be used in the name of variable.
(6) The maximum numbers of characters permitted in the name of variable is 31 characters.
|How to declare and initialize variable?
In C language we must have to declare variable before it is used in the program.
The declaration of variable instruct compiler to allocate specified amount of memory to hold the value of variable and assign name to that allocated memory.
The general syntax for declaring variable is given below:
Data Type Variable_Name;
The process of assigning value to the variable is known as variable initialization.
The general syntax for initializing variable is given below:
VariableName = Value;
a = 5;
Constant means fixed value.
The value of the constant can not change during the execution of the program.
For Example, the value of PI is fixed which can not be changed. So it can be declared as a constant.
The general syntax for defining constant in the program is given below:
const Constant_Name = value;
const PI = 3.14;
(1)Constant must be assigned value at the time of its declaration.
(2)Constant can be integer, floating point, character or string.
(3)Character constant must enclose between single inverted comma and string constant must enclose between double inverted comma.
Following are some examples of constant:
const SIZE = 10; // Integer Constant
const PI = 3.14; // Floating Point Constant
const SYMBOL = ‘C’; // Character Constant
const NAME = “Programming In C”; // String Constant
|Backslash Charcater Constant (Escape Sequance)
Escape Sequence is a combination of backslash and a single character.
It is also known as backslash character constant.
Escape Sequence is used for following two purpose:
(1) To display non printable characters such as new line, tab etc…
(2) To display those characters having special meanings.
List of Escape Sequence in C:
|| Bell (alert)
|| Form feed
|| New line
|| Carriage return
|| Horizontal tab
|| Vertical tab
|| Single quotation mark
|| Double quotation mark
|| Literal question mark
The general syntax of defining symbolic constant is given below:
#define Symbolic_Constant_Name value
(1) #define is a preprocessor directive which is used to define symbolic constant.
(2) Symbolic_Constant_Name is a name of the constant, which we want to define.
(3) Value of the constant represents the value, which we want to assign to the constant.
#define PI 3.14
Rules of defining symbolic constant:
(1) The first character must be #.
(2) There should be no blank space between the # and define.
(3) There should be blank space between #define and symbolic constant name and between the symbolic constant name and the value.
(4) The statement can not end with a semicolon.
(5) There is no need to define data type for symbolic constant.
(6) Once we define value for the symbolic constant we can not assign it with other value in the program.