Variables in C | Set 2

By | February 25, 2024

Prerequisite – Variables in C | Set 1
In programming, a variable is a container (storage area) to hold data. A variable is nothing but a name given to a storage area that our programs can manipulate.

Each variable in C has a specific type. That is, every variable declared must be assigned as a certain type of variable. In Standard C there are four basic data types. They are int, char, float, and double.

Rules for naming a variable

  1. A variable name can only have letters (both uppercase and lowercase letters), digits and underscore.
  2. The first letter of a variable should be either a letter or an underscore.
  3. Special characters like #, $ are not allowed.
  4. Variable names are case sensitive.
  5. Reserved Keyword should not used as variable name.
  6. 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.

Some examples of valid (but not very descriptive) C variable names:

foo
Bar
BAZ
foo_bar
_foo42
_
QuUx 

Some examples of invalid C variable names:

2foo    (must not begin with a digit)
my foo  (spaces not allowed in names)
$foo    ($ not allowed -- only letters, and _)
while   (language keywords cannot be used as names) 

Note that “while” is reserved keyword so can not be used as name of variable.

  1. Declaration of variables must be done before they are used in the program. E.g., extern int a;
  2. Defining a variable means the compiler has to now assign a storage to the variable because it will be used in the program. E.g., int a;
  3. Initializing a variable means to provide it with a value. E.g., int a = 50;

Examples :

#include <stdio.h>

// Variable declaration (optional)
extern int a ;

int main () {

    // variable definition: 
    int a,
 
    // actual initialization 
    a = 10;
   
    // define and intialize in one line
    int b = 50 ;
 
    // remaining code goes here
    
    return 0;
}

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

These are given as follows.

1. Local Variable :
Local variables are used inside the function or block and its scope is limited to function or block. It cannot be used outside the block.

int foo() {  

// local variable
int x = 10 ;  
}  

You must have to initialize the local variable before it is used.

2. Global Variable :
Global variable is declared outside the function or block. It is declared at the starting of program and available to all the functions.

#include <stdio.h> 

// global varible
int a = 10 ; 
void foo() 
{ 

  // used in inside this function
  printf("%d\n" , a); 
} 
void goo() 
{ 

  // also used in inside this function
  printf("%d\n" , a); 
} 

// driver code
int main() { 
  
  // first function
  foo();
  // second function 
  goo(); 

  return 0; 
} 

3. Static Variable :
Static variable retains its value between multiple function calls or recursion calls. It is declared with the static keyword.

#include <stdio.h>

void foo()
{
    // local variable
    int a = 10;
    // static variable
    static int b = 20;

    a += 5;
    b += 10;

    printf("a = %d, b = %d\n", a, b);
}


int main()
{
    int i;

    for (i = 0; i < 5; ++i)
        foo();
}

Output :

a = 15, b = 30
a = 15, b = 40
a = 15, b = 50
a = 15, b = 60
a = 15, b = 70

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

int main() {  
    
  //local variable (also automatic by default) 
  int a=10;  
  
  // automatic variable used auto keyword
  auto int b = 20; 
}  

5. External Variable :
External variable can be shared between multiple C files. We can declare external variable using ‘extern’ keyword.

  // first file
  myfile.h
  // external variable (also global)
  extern int a = 10 ;  

  // another file 
  another.c
  #include "myfile.h"  
  #include <stdio.h>  
  void foo() {  
  printf("Global variable: %d", global_variable);  
  }  

Note that ‘extern’ keyword is used with the variable for its identification as a global variable.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.