Variable in C

 



Variable in C

Every C program is consists of functions and variables, whatever its size. a variable is an entity that changes during the execution of the program.

Variable is the name given to the memory location in which data/value  is stored and these stored values are used during the execution of the program.

All variables  we need to be declared before they are used, mostly at the beginning of the function before any executable statements.

When we declare the variable must announce the properties of  that variables; it consists of a data type name and a list of variables.

A variable definition specifies a data type and contains a list variable of that type as follows −

 Data_type variable_list;

int    a,b,c;

char   c, ch;

float  a, colour;

double b;

in this example int indicated is an integer data type,  char indicated is a character data type, float and double also data type.

Rules for defining variables:

  1. The variable name can be a combination of alphabets(  Alphabets can be in capital or lower case), digits, and underscore
  2. Variable can  start with alphabets or underscore _ symbol
  3. Alphabets can be in capital or lower case
  4. Never start a variable name with the number.
  5. The maximum length allowed to declare is 32 characters
  6. You can not use a keyword as a variable name.
  7. No special symbols allowed except underscore

Declaring Variables:

Variables are declared in three basic places:

Inside functions: local variables

In the definition of function parameters: Global Variable

Outside of the functions: Global Variable 

Types of Variables:

Global variables:

 Global variables are declared above main () function or we can say out of the function.  All the function can access the value of a global variable throughout the program. in the following way:

int value=30;  / *global variable */

 main()

{

}

Local variables:

Local Varibles are declared inside a function are called as local variables You must have to initialize the local variable before it is used.

void main(){  

int x=20;  / *local variable*/  

  }  

 Static Variable 

A static variable is declared with the static keyword and it retains its value for multiple functions call.
void main(){  

Static int x=20;  / * static variable*/  

}  

Automatic Variable

Automatic variables are similar as local variables. All variables in C which are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

void function()

{

  int a=20;//) 

  auto int b=10; /*automatic variable*/

}

External Variable 

The external variable is shared between multiple C files. We need to store the external variables in a separate file and file  extension is .h example: myfile.h  and We can declare an external variables using extern keyword.

Example: 

  myfile.h

   extern int x=20;/*external variable*/

 

  program11.c

  #include "myfile.h" 

  #include <stdio.h> 

  void mail(){ 

   }

Previous
Next Post »