Variables - C programming language

Variables

In C language, variables are the storage place where some form of data is stored. Different variables require different amount of memory on which a set of operations is applied. A variable name cannot start with a number. It can consist of alphabets, number, underscore “_”. Here is the syntax of declaring variables in C language.

  • A variable is defined as a meaningful name given to the data storage location in computer memory .
  •  A variable has to be declare before it is used .A variable can either store numeric data or character data.
  • Variables names should always be meaningful and must be reflect the purpose of their usage. 
  • Variables declaration always ends with a semicolon .


type variable_name 
Here is the syntax of multiple variables declaration in C language, type variable_name1, variable_name2, variable_name3; The following is an example of variables in C language, 

Some examples are −

extern int d = 3, f = 5;    // declaration of d and f.
int d = 3, f = 5;         // definition and initializing d and f.
byte z = 22;                // definition and initializes z.
char x = 'x';               // the variable x has the value 'x'.

Types of Variables in C

There are many types of variables in c:

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

Local Variable

A variable that is declared inside the function or block is called a local variable.

It must be declared at the start of the block.

  1. void function1(){  
  2. int x=10;//local variable  
  3. }  

You must have to initialize the local variable before it is used.

Global Variable

A variable that is declared outside the function or block is called a global variable. Any function can change the value of the global variable. It is available to all the functions.

It must be declared at the start of the block.

  1. int value=20;//global variable  
  2. void function1(){  
  3. int x=10;//local variable  
  4. }  


Static Variable

A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

  1. void function1(){  
  2. int x=10;//local variable  
  3. static int y=10;//static variable  
  4. x=x+1;  
  5. y=y+1;  
  6. printf("%d,%d",x,y);  
  7. }  

If you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.

Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

  1. void main(){  
  2. int x=10;//local variable (also automatic)  
  3. auto int y=20;//automatic variable  

 External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.

myfile.h

  1. extern int x=10;//external variable (also global)  

Rules for variables 

  • It can not have special character except the underscore "_".
  • It can not be a keyword.
  • It can not have spaces in between.
  • It must begin with a alphabates or an underscore. 
  • C is also a case sensitive language so here "Class" & "class" both are different. 
  • It can not contain more then 31 characters.

Post a Comment

0 Comments