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 :
- Global variables
- 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.
Looking for a Fresher Job? Post Your Resume Free !!!
More Articles From "C Programming" Category
- Derived Data Types in C Programming Language
- C Language Operator Types: Operators in C
- C Programming Primary Constants: Integer, Real & Character
- Constants in C Programming Language: Primary & Secondary
- Storage Classes in C Programming Language: Auto, Register, Extern & Static Storage Types
- C Data Types: Range & Size
- Type Modifiers in C Language: signed, unsigned, long & short
- C Language Fundamental Data Types: Int, Float, Char, Double
- C Language Data Types
- C Language Keywords