What is the Use of Variable in C?
Knowing the meaning and definition of a variable one might wonder what is the use of defining the variables in C ? Well, for the efficient working and execution of the program we use variables. Every variable has a type and size. Variables store data that can later be used by the programmer or user to complete the execution of a particular task or code.
Declaration of Variable in C
Variable declaration in C provides the compiler with the credibility that a particular storage area is assigned some name along with the size and type.
The syntax for declaring a variable in C:
data type variable name;
For example:
int marks;
float weight;
Here int and float represent the type of variable respectively and marks and weight represent variables' names followed by semicolons.
Initialization of Variable in C
One can also initialize the value of a variable that is assigning a valid value
(belonging to the specified type and size of the variable) to the variable is termed as initialization. The syntax for initializing a variable is: type variable name= value;
For example:
int marks=100;
Float weight = 56.8;
Here int and float represent the type of variable respectively and marks and weight represent variables’ names with values assigned to them.
Data Types of Variable in C
In C, variables are divided based on various categories based on size, scope, lifetime data types etc. On the basis of data types we have two categories:
- Primitive data types
- User-defined data types
- Derived data types
- Primitive data types: It is mandatory to assign a data type to a variable in C, which defines the type of data that can be stored under the variable name. Primitive data types in C are shown in the below table:
Data type | Size |
char | 1 byte |
int | 2 bytes |
float | 4 bytes |
double | 8 bytes |
2. User-defined data types: These data types have been designed for ease of the user purpose. In C we have the following three types of user-defined data types:
- Structure
- Union
- Enum
3. Derived data types: In C we have derived data types that can contain multiple homogenous data at once. Following are the derived data types in C:
- Array
- Functions
- Pointers
- Storage classes of variables in C
Every variable in C has some characteristics like the data type, scope lifetime etc. The storage classes deal with the scope and lifetime of the variable. The storage classes are mainly classified into four categories:
- Auto storage class
- Register storage class
- Static storage class
- Extern storage class
Auto Storage Class
All the local variables are auto variables by default.
If an auto variable is not initialized then the default contains a garbage value. For every function call, the auto variable is recreated and re-initialized. It has local scope.
Syntax
auto data_type variable_name;
For example: auto int marks;
Register Storage Class
The keyword register is just a request to the compiler to allocate memory in the registers instead of RAM, if the free registers are available then memory will be allocated in the registers otherwise it will be allocated in the RAM only.
Syntax
register data_type variable_name;
For example: register int marks;
Static Storage Class
For static variables, the memory will be allocated only once at compile time, if it is not initialized the default contains zero. A static variable will persist in its value even after the destruction of several function calls.
Syntax
static data_type variable_name;
For example: static int marks;
Extern Storage Class
The variable declared extern will not be allocated any memory, it will make use of the outside memory(global variables).
Syntax
extern data_type variable_name;
For example: extern int marks;
Get unlimited access to 21+ structured Live Courses all 112+ mock tests with Online Classroom Program for GATE CS & PSU Exams:
Click here to avail Online Classroom Program for Computer Science Engineering
Thanks
Comments
write a comment