Variable is a storage place to hold data which has some memory allocated to it. It is used to store data.  it can be reused many times. We can change value of a variable during execution of a program. Different types of variables require different amounts of memory.

C is a strongly typed language. This means that the variable type cannot be changed once it is declared.

Load WordPress Sites in as fast as 37ms!

Syntax to declare a variable:-

type variable_name;
type variable_neme_1,variable_neme_2,variable_neme_3;

Example:-

int a b,c;
float d;
char e;

Here a, b,c,d,e are variables and int, float, char are data types.

Load WordPress Sites in as fast as 37ms!

The line int a, b, c; declares and defines the variables a, b, and c; which instruct the compiler to create variables named a, b and c of data type int.

Rules for naming a variable

  • A variable name can start with the alphabet, and underscore only. It can’t start with a digit.
  • No whitespace is allowed within the variable name.
  • A variable can have alphabets, digits, and underscore.
  • A variable name must not be any reserved word or keyword, e.g. int, char, void, goto , etc.

**There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.

Load WordPress Sites in as fast as 37ms!

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 and used inside the function or block is called local variable. It’s scope is limited to function or block. It cannot be used outside the block. We must have to initialize the local variable before it is used.

#include <stdio.h>
void function1() {
  int a = 5; // local variable
  printf ("value of local variable a is = %d",a);
}
 
int main()
{
  function1();
return 0;
}

Global Variable

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

Example

Load WordPress Sites in as fast as 37ms!
#include <stdio.h>
int b = 10; // Global Varoable
void function1() {
  int a = 5; // local variable
  printf ("value of local variable a is = %d\n",a);
  printf ("value of Globle variable b is = %d\n",b);
  b= b+a;
  printf ("value of Global variable b is = %d\n",b);
  
}
 
int main()
{
  function1();
return 0;
}

Output

value of local variable a is = 5
value of Globle variable b is = 10
value of Global variable b is = 15

Static Variable

A variable that retains its value between multiple function calls is known as static variable. It is declared with the static keyword.

Load WordPress Sites in as fast as 37ms!

Example

#include <stdio.h>
void function1(){ 
int a = 10;//local variable 
static int b = 20;//static variable 
a = a + 10; 
b = b + 10; 
printf("\n%d,%d",a,b); 
} 
int main() {
 
  function1();
  function1();
  function1();
  function1();
  function1();
  return 0;
}

Output

20,30
20,40
20,50
20,60
20,70

Automatic Variable

Automatic variables are similar as local variables. All variables in C that are declared inside the block, are automatic variables. We use auto keyword to define Automatic variable.

Load WordPress Sites in as fast as 37ms!

External Variable

External Variable can be shared in multiple C source files. We need to use an extern Keyword to declare an external variable.

Example

externvariable.h
  extern int a=5;//external variable  
   
  testprogram.c
  #include "externvariable.h"  
  #include <stdio.h>  
  void main(){  
  printf("external variable: %d\n", a);  
  }

Output:-

external variable: 5

Difference between Identifiers and Variables in C

VariableIdentifier
A variable is a name that points to a memory location.Identifiers are used for name a variable, a function, a class, a structure, a union.
It is the basic unit of storage in a program.It is a string of alphanumeric characters that begins with an alphabet, or an underscore( _ )
They help allot a unique name to a specific memory location.It is created to give a unique name to an entity.
Sharing is Caring
Load WordPress Sites in as fast as 37ms!
Scroll to Top