Variables in C | Set 2

Last Updated :

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.

Comment
Next Article
CPP_Programmer Published 18 Aug, 2020 · 4 min read
Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read