C – Variables

Home/C - Tutorial/C – Variables
C – Variables 2017-07-19T15:21:29+00:00

Variables :

  • Variables are means for location in memory used by a program to store data. The size of that block depends upon the range over which the variable is allowed to vary.
  • For example, on personal computer the size of an integer variable is two bytes, and that of a long integer is four bytes.

A variable region is temporarily remember a number or string value, such as covered by the program. To identify the variables, you have a name unique to every single variable. This is called a variable name. Before using a variable, use variables to what is called a variable declaration that you have to reveal the names and data types that can be stored in the variable variable.

The format for declaring a variable in C.

[Storage-class] type data variable name [= initial value];
  • Storage class and the initial value can be omitted.
  • The same data type and storage class variable can be declared, separated by commas.
[Storage-class] type data variable name [= initial value] variable [= initial value] variable [= initial value];
  • In C the size of a variable type such as an integer need not be the same on all types of machines. When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type character with the name i by writing:

char i;

  • On seeing the “char” part of this statement the compiler sets aside one bytes of memory to hold the value of the character. It also sets up a symbol table. In that table it adds the symbol i and the relative address in memory where those one byte was set aside. Thus, later if we write: i = ‘x’; we expect that,at run time when this statement is executed, the value ‘x’ will be placed in that memory location reserved for the storage of the value of i.

Following are the rules for naming the variables:

  1. All variables must be declared before they can appear in executable statement.
  2.  A declaration consists of a data type followed by one or more variable names separated by commas.Example: int a,b,c;
  3. Variables can be distributed among declarations in any fashion. The above declaration can be written asint a;int b,c;
  4. Integer type variables can be declared to be short integer for smaller integer quantities or long integer for larger integer quantities.Example:short int a,b,c;long int a,b,c;
  5. An integer variable can also be declared to be un signed by writing unsigned int.

    Example: unsigned int;


Output :

Value of x = 10

Value of x = 20

Prev Next