C Variables and Types of Variables in C

C variable is a data name that may be used to store a data value. The value of the C variable may get change in the program and it can be reused many times.
syntax to declare a variable:
type variable name = value;

Example of C variables:

int i = 1, j = 2;               // Declaration and initializing i and j.
float f = 10.53;              //Declaration and initializing f.

Rules for defining variables:

  • C variable can have alphabets, digits, and underscore and must begin with a letter.
  • C variable name can start with the alphabet, and underscore only can have both upper and lower case letters. It can't start with a digit.
  • No whitespace is allowed within the variable name.
  • C variable name must not be any reserved word (or) keyword, e.g. int, float, char, etc.

Types of Variables in C: The types of variables in c are shown below:
  • The local variable.
  • Global variable.
  • The static variable.
  • Automatic variable.
  • External variable.

Local Variable: A variable that is declared inside the function or block is called a local variable. The local variable must be declared at the start of the block. And local variable must have to initialize before it is used.

Example of C Local variable:

void functionA()
int i=10; // "i" is the local variable.
Global Variable: Any variable that is declared outside the function or block is called a global variable. A global variable must be declared at the start of the function (or) block and it is available to all the functions. Any function can change the value of the global variable.

Example of C Global variable:

int i = 20;    // Here "i" is the global variable  
void function1()
 int j =10;    //Here "j" is the local variable  

Static variable: The static variable has a property of preserving their value even after they are out of their scope. Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.
static datatype varname = varvalue; 

Automatic variable: The variables which are declared inside a block are known as automatic these variables allocate memory automatically upon entry to that block and free the occupied memory upon exit from that block. These variables have local scope to that block only which means these can be accessed in which variable declared. Keyword 'auto' may be used to declare an automatic variable but we can declare these variables without using 'auto' keywords.
 Example: Below Both x, y are automatic variables
  int main()
    auto int x;
    int y;
    return 0;
External variables: External variables are also known as global variables. These variables are defined outside the function. These variables are available globally throughout the function execution. The value of global variables can be modified by the functions. “extern” keyword is used to declare and define the external variables. About the declaration shown below:
  • Declaration of a variable or function simply declares that the variable or function exists somewhere in the program, but the memory is not allocated for them. The declaration of a variable or function serves an important role–it tells the program what its type is going to be. In the case of function declarations, it also tells the program the arguments, their data types, the order of those arguments, and the return type of the function. So that’s all about the declaration.
  • Coming to the definition, when we define a variable or function, in addition to everything that a declaration does, it also allocates memory for that variable or function. Therefore, we can think of definition as a superset of the declaration (or declaration as a subset of definition).
#include <stdio.h>
extern int i = 24;
int j = 8;
int main() {
   auto int a = 32;
   extern int j;
   printf("The value of auto variable : %d\n", a);
   printf("The value of extern variables i and j : %d,%d\n",i,j);
   x = 15;
   printf("The value of modified extern variable i : %d\n",i);
   return 0;

Our Services

Search This Blog

  • ()
Powered by Blogger.

C Variables and Types of Variables in C

C variable is a data name that may be used to store a data value. The value of the C variable may get change in the program and it can be r...