Search Placementyogi.com
 

C Variables

  • A 'Variable' is a name given to a memory location and it's value may change during program execution.
  • Each variable in C has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

          Example :  int x = 10; 

 

In above diagram x is a name given to the memory location whose address is 0X2001009 and the type of value that can be stored at this memory location is integer constant .i.e 10.

Now x=20, this would overwrite the earlier value 10 because a memory location can hold only one value at a time. Since the location whose name is x can hold different values at different times, x is know as variable. 

Rules of constructing variable names in C

  1. A variable name is any combination of alphabets, digits or underscores of max size of 31 characters (Some compiler allow more than 31 characters but for safer side always stick to 31 characters).
  2. First character should not be a number. It can be alphabets or underscore. 
  3. No commas, blanks or any special symbols other than an underscore (as in my_value) can be used.
Example : 
my_variable21  /* valid */
21variable     /* Invalid : first character can not be a number */
g#roup         /* Invalid : special symbol (#) are not allowed  */         
_group         /* valid */
name.age       /* invalid : special symbol (#) are not allowed  */
_X             /* valid */

Variable Declaration, Definition and Initialization in C 

All variables should be declared in program before using it in any statement so that compiler and runtime environment can determine memory depending on its type.

Syntax  :  datatype variable_name;

Example :  int x;     /*int is datatype and x is the variable*/
           int y;
		or int x,y;   /* First two declarations are combined in one single stmt*/ 
           float sum;
		   char name;

Note: A variable declaration does not allocate any memory for variable but variable definition allocate required memory for that variable.

int x;  Actually this is both declaration and definition so memory (4 bytes) is allocated for this variable.

Assigning some value to a variable is called variable initialization. Equal sign(=) followed by an constant expression is used for initialization.

Example : int x=10;  /* Declaration, Definition and Initialization in single stmt */ 
          int y=20;
	  or  int x=10,y=20;
	      float 10.5;
		  char name='A';

Lvalues and Rvalues in C

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.

int x=100;  /* Statement 1 : valid*/
int y=x;    /* Statement 2 : valid*/
100=200;    /* Statement 3 : invalid */ 

In Statement 1, x is lvalue and 100 is rvalue.

In Statement 2, x is rvalue and y is rvalue.

In Statement 3, 100 is numerical literals i.e. rvalue, so it can not be on left side of assignment.

 

Now we can say 

lvalue : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment(=).

rvalue : An expression that is an rvalue may appear on the right- but not left-hand side of an assignment(=).

 

Banner to LogicGuns.com

Hide Page Information

c variables, types of c variables

c variables, types of c variables, c variables tutorial, primary variables, secondary variables