Variable Types in C Programming Language

Posted on January 25, 2010

Variable Types in C Programming Language

Variables are names which are declared in order to store a single value that can either be an integer or a character. It is must to declare the type of the variable also i.e int, char etc. Generally there are two types of variables namely :

  1. Global variables
  2. Local variables

These variables are classified on the basis of their place of declaration.

Global Variables:

  • If a variable is declared outside all the functions, then it is said to be global variable.
  • A global variable is available to all the functions and each part of the program.
  • When a program is executed then a global variable comes into consideration.
  • A global variable is destroyed when the program is terminated.
  • Global variables can be accessed from any part of the file or program.

Example:

#include
int a;
int b;
char ch;
void main()
{
printf(“\n enter the numbers :”);
scanf(“%d%d”,a,b);
printf(“\n enter the character”);
scanf(“%c”,ch);
}

Now from the above example we can see that ‘a’ and ‘b’ are integer variables and ‘ch’ is a character variable which is declared outside the main function. Therefore, these variables are known as global variables. These variables can be accessed from any part of the program.

Local Variables:

  • Local variables are the ones that are defined within a function.
  • A local variable comes into existence when any function is entered.
  • A local variable is terminated when we exits from the function.
  • These variables cannot be accessed from outside the function.
  • A local variable needs to be declared every time after the function call.

Example:

#include
int main()
{
int i;
int j;
int add(i,j);
}
int add(int x , int y)
{
int result;
result = x + y;
printf(“%d the result is:”,result);
return result;
}

Now from the above code we can see that all the variables are declared within the function and these variables will be executed when we enter any function. Therefore, these variables are not allowed to be accessed from any part of the code.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Looking for a Fresher Job? Post Your Resume Free !!!

More Articles From "C Programming" Category

home | top

TopOfBlogs