Variables in C Language
Variable is a name of the memory location where we can store any data. It can store only single data (Latest data) at a time. In C, a variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function.
A declaration begins with the type, followed by the name of one or more variables. For example,
DataType Name_of_Variable_Name;
int a,b,c;
Variable Names in C Language:
Every variable has a name and a value. The name identifies the variable, the value stores data. There is a limitation on what these names can be. Every variable name in C must start with a letter; the rest of the name can consist of letters, numbers and underscore characters. C recognizes upper and lower case characters as being different. Finally, you cannot use any of C's keywords like main, while, switch etc as variable names.
Examples of legal variable names include:
x result outfile bestyet
x1 x2 out_file best_yet
power impetus gamma hi_score
It is conventional to avoid the use of capital letters in variable names. These are used for names of constants. Some old implementations of C only use the first 8 characters of a variable name.
Local Variables in C Language:
Local variables are declared within the body of a function, and can only be used within that function only.
Syntex:
void main( )
{
int a,b,c;
}
void fun1()
{
int x,y,z;
}
Here a,b,c are the local variable of void main() function and it can’t be used within fun1() Function. And x, y and z are local variable of fun1().
Global Variable in C Language:
A global variable declaration looks normal but is located outside any of the program's functions. This is usually done at the beginning of the program file but after preprocessor directives. The variable is not declared again in the body of the functions which access it.
Syntax:
#include<stdio.h>
int a,b,c;
void main()
{
}
Void fun1()
{
}
Here a,b,c are global variable .and these variable cab be accessed (used) within a whole program.
Post A Comment:
0 comments: