Variables, Scope of Variables and Constants of C Language.

A variables is nothing but a name given to storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable’s memory; the range of value that can be stored within that memory; and the set of operations that can be applied to the variable.
Rules for naming a variable
  • Every variable name should start with alphabets or underscore (_)
  • Variable name can consist of Capital letter A-Z, lowercase letter a-z , digits 0-9 and the underscore character
  • Blank spaces cannot be used in variable names.
  • Instead (_) character no special character are allowed in C variable .
  • C keywords can not be used as variable names.
  • Variable names are case sensitive.
  • Maximum length of variable is 8 characters  depend on compiler and operation system.
Variable Definition in C 
Syntax: Type variable-name; 
Type variable_name, variable_name, variable_name;
Int width, height=5;
Char letter=’A’;
Float age, area;
Double d;
/*actual initialization*/
Width = 10;
Age = 26.5;
 Variable Assignment
Variable assignment is process of assigning a value to variable.
                Int width = 54
                Int age = 29;

Scope of variable
In C language, a variable can be either of global or local scope.
Global variable
                       Global variable are defined outside of all the functions, generally on top of the program. The global variable will hold their value throughout the life-time of your program.
Local variable
                    A local variable is declared within the body of a function or block. Local variable only use within the function or block where it is declare.
Example of Global and local variable
Int a;// global variable
Void main()
Int b;//local variable 
a=10, b=20;
printf(“value  of a : %d”a);
printf(“value of b : %d”,b);
Value of a :10
Value of b : 20

  • Constants are like variable, except that value never changes during execution once defined.
  • Constants are also called literals. 
  • Constants can be any of the data types.
  • If is considered best practice define constants using only upper-case names.
Constant Definition in C
Syntax: const type constant_name;
Void main()
Constant SIDE=10;
Int area;
printf(“The area of the square with side:%d is : %sd sq. unirs”
.SIDE, area );
Type of constant in C language
 Primary Constant 
Integer Constant
Its refers to sequence of digits.
15, -245, 0, 97688, +24, 037, 0 ×6
Real constant
The numbers containing fractional parts like 99.25 are called real of floating points constant.
Character constant 
A single character enclosed within a pair of single quote marks.
Example: “x” , “5”, “;”
Backlash character constant
C support some special backlash constant that are used in output functions.  

/b back space
/f form frre
/n new line
/r carriage return
/t horizonral tab
/v vertical tab
/' single quote
/" double quote
// backlash
/0 A collection of related variables of the same and/ or different data types.
String constant
 A string constant is a sequence of character enclosed within quotes. Example: “HELLO !”, “2005”, “2=1”
 Secondary Constant
  • Array
  • Pointer
  • Structure
  • Union
  • Enum

You might like

Post a Comment