Variables in c
2 minute read
Variables in c
In C language, when we want to use some data value in to the program, we store it in a memory space and give the name to the memory space so that it becomes easier to access it.
The
naming of an Address is known as variable. Variable is the name of memory location. Variables means changeable, we can change value of a variable during execution of a program. A programmer can choose a meaningful variable name. Example : average, height, age, total etc.
Rules for naming a variable
1. Variable name can not start with a digit.
2. Variable name can consist of digits,alphabets and special symbols like underscore
3. Blank spaces are not allowed in variable name.
4. Keywords are not allowed as variable name.
5. Lower and upper case names are treated as different, as C is case-sensitive language.
Syntax
Type variablename
Example
float salary
Storage classes in c
A storage class defines the scope (visibility) and life of variables within a C Program. There are four types of storage classes in a C program −
- auto
- register
- static
- extern
The auto Storage Class
The auto storage class means local variable it is the default storage class.{
int mount;
auto int month;
}
In the above example we defines two variables with in the same storage class. 'auto' can only be used within the braces, i.e., local variables.
The register Storage Class
The register storage class is also a local variables but it is stored in a register instead of RAM. It means variable's maximum size equal to the register size. The register variables use when the quick access variable require such as counters.{
register int miles;
}
The static Storage Class
The static storage class tell the compiler that a local variable life is in hall the program. Therefore static allows local variables to maintain their values between function calls. The static modifier also applied to global variables.
Example
Static int a=5;
Extern storage class
Extern stands for external storage class. Extern storage class is used when we have global variables which are shared between two or more files.
Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file.
These variables are accessible throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original file
extern int count;