Variables in C

A variable is a name of the memory location. It is used to store data. It is way of reserving memory to hold some data and assign names to them so that we don't have to remember the numbers like 316976 and instead we can use the memory location by simply referring to the variable name. Its value can be changed, and it can be reused many times.

A variable can contain different types of data and each type may have different space requirement. The data type of a variable signifies the type of data it contains and the space it requires to hold it.

Variable declaration
data_type variable_name;
Variable initialization
data_type variable_name= value;

The example of declaring the variable is given below:

int a;
float b;
char c;

Here, a, b, c are variables. The int, float, char are the data types.

We can also provide values while declaring the variables as given below:

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

Rules for defining variables

A variable may be defined using any uppercase or lowercase character, a numerical digit (0 through 9), and the underscore character (_). The first character of the variable may not be a numerical digit or underscore. Variable names are case sensitive.

Valid variable names:

int a;
int _ab;
int a30;

Invalid variable names:

int 2;
int a b;
int long;

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 and you must be initialize the local variable before it is used.

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

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.

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

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

It retains its value between multiple function calls.

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

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.

void main(){
int x=10;//local variable (also automatic)
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, use extern keyword.


extern int x=10;//external variable (also global)
#include "myfile.h"
#include <stdio.h>
void printValue(){
    printf("Global variable: %d", global_variable);
Scope of variable

The scope of the variable (where it can be used), is determined by where it is defined.

file scope
If it is defined outside any block or list of parameters, then it has file scope. This means it may be accessed anywhere in the current source code file. This is normally called a global variable and is normally defined at the top of the source code. All other types of variables are local variables.
block scope

If a variable is defined in a block (encapsulated with { and }), then its scope begins when the variable is defined and ends when it hits the terminating }. This is called block scope.

function prototype scope

If the variable is defined in a function, then the variable may only be accessed in that function. This is called function prototype scope.

Access to variables outside of their file scope can be made by using linkage. Linkage is done by placing the keyword extern prior to a variable declaration. This allows a variable that is defined in another source code file to be accessed.
Life of a variable
Automatic storage duration

Variables defined within a function scope have automatic storage duration. The life of the variable is determined by the life of the function. Space is allocated at the beginning of the function and terminated at the end of the function.

Static storage duration

Static storage duration can be obtained by placing the keyword static in front of the variable declaration. This causes the variable's space to be allocated when the program starts up and is kept during the life of the program. The value of the variable is preserved during subsequent calls to the function that defines it. Variables with file scope are automatically static variables.

Related Tutorial
Follow Us #
Contents +