Home >>C Tutorial >Variables in C

Variables in C

Variables in C

The variable in C is basically the name of the memory location that is used to store data and the value can be changed hence, it can be reused multiple times.

The memory location is represented through a symbol in order to get identified easily.

Here is the syntax to declare a variable:

type variable_name;

Here is an example to declare the variable:

int x;  
float y;  
char z;

In the above example, x,y,z are variables and the int, float, char are the data types.

Users can also provide values while declaring the variables as depicted in the below example:

//declaring 2 variable of integer type 
int a=10,b=20; 
float f=20.8;  
char c='A';  

The Rules for defining the variables in C

  • The variables in C can contain alphabets, digits, and underscore.
  • The name of a variable can start with the alphabet and underscore only. The name can never start with a digit.
  • Please note that there is no whitespace allowed within the name of a variable.
  • The variable name must not be any reserved word or keyword, e.g. int, float, etc.

Here are the examples depicting the valid variable names and the invalid variables name:
Valid names of the variable:

int m;  
int _mn;  
int t40; 

Invalid name of the variables

Int 7;  
int x y;  
int short;

Variable Types in C

Variables in C have many types but here are the main types of the variables in C:

  • static variable
  • global variable
  • local variable
  • external variable
  • automatic variable

1. Static Variable in C

The variable in C that is declared with the static keyword is known as a static variable. Between multiple function calls it retains its value.

Here is an example depicting the static variable in C

void function1()
{  
int x=11;//local variable  
static int y=10;//static variable  
x=x+1;  
y=y+1;  
printf("%d,%d",x,y);  
}  

In case, this function is called many times then the local variable will print the exact same value for each function call, e.g, 12,12,12 and the list will continue. The point to be noted here is that the static variable will print the incremented value in each function call, e.g. 12, 13, and continue.

2. Global variable in C

A global variable is C is declared from outside the function or block. The value of the global variable can be changed by any function. Global variable is available to all the functions. Please note that it must be declared at the starting of the block.

Here is an example depicting the Global Variable in C

int value=20;//global variable  
void function1()
{  
int x=10;//local variable  
}  

3. Local Variable in C

A local variable in C is a variable that is declared inside the function or block.
Please note that the local variable must be declared at the start of the block.

Here is an example depicting the local variable in C:

void function1()
{  
int x=10;//local variable  
}  

4. External Variable in C

By using an external variable a variable can be shared in multiple C source files. In order to declare an external variable, use of extern keyword is necessary.

Here is an example of External variable in C: myfile.h

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

program1.c

#include "myfile.h"  
#include   
void printValue()
{  
    printf("Global variable: %d", x);  
}  

5. Automatic Variable in C

All the variables in C that are declared inside the block are known as automatic variables by default. By using an auto keyword an automatic variable can be declared explicitly.

Here is an example of Automatic variable in C:

void main()
{  
int x=10;//local variable (also automatic)  
auto int y=20;//automatic variable  
}  

Popular Tutorials