What are Variables in C? Meaning, Usage, Declaration, Examples

By Mona Kumari|Updated : October 11th, 2022

A Variable in C is a name given to a memory location where some data is stored. We can now refer to this data multiple times, change its value and use it when required. One can understand a variable as a location where something is residing. Now let’s understand what a variable in C means. In layman’s language, the city one resides in acts as a variable; for example, say person x resides in Delhi, then this acts as a variable name because one uses this information multiple times, and person x can also change its city from Delhi to say Kolkata or any other place.

Variable in C PDF

In the article, we will dig deep into what variable in C language refers to according to the GATE syllabus for CS/IT. As we are already familiar with the meaning of a variable, It tells the compiler how much storage it should be creating for the given variable and where it should create the storage. Intuitively, the variable definition helps in specifying the data type.

Download Formulas for GATE Computer Science Engineering - Programming & Data Structures

What are Variables in C?

A variable in C acts like a container that stores information or data values. Once the data values are stored in the variable, we can use this variable as many times as we want. A variable in C has some characteristics, like its lifetime, scope, and range of values it can store.

A variable in C can take different types of values like integer, character, string, float, and double values, which are important parts of the GATE CSE syllabus. Each variable is associated with a data type that allows for storing values of a particular type. 

Download Formulas for GATE Computer Science Engineering - Discrete Mathematics

Variable in C Meaning

The name of a memory address we use to store data is all that a variable means. In C or any other programming language, we can modify a variable's value and reuse it several times. Variables play an integral part in any source code. Once a variable is declared, we can perform multiple tasks using it within the same source code.

Variable in C Example

As we already know, every variable declared in the program is associated with a data type. It may take a range of values depending on the data type. Along with the data type, it may also use storage classes that precisely define the scope and lifetime of a variable. And these data types are essential as per the GATE exam.

Let us see an example of variables in C:

#include<stdio.h>

int main()

{

int day= 12;

char grade= 'A';

float marks= 98.7;

string name="Saif";

printf("%d\n%c\n%f\n%s\n", day, grade, marks, name);

return 0;

}

The above program declares and initializes four variables that store data values according to the associated data type. The variables here are day, grade, marks, and name.

Download Formulas for GATE Computer Science Engineering - Algorithms

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.

The importance of variables can also be seen in the GATE question papers. Variables store data that the programmer or user can later use 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.

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 initialization. The syntax for initializing a variable is: type variable name= value; 

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 Variables in C

In C, variables are divided based on various categories based on size, scope, lifetime data types, etc. Based on data types, we have two categories:

1. Primitive data types: It is mandatory to assign a data type to a variable in C, which defines the type of data stored under the variable name. Primitive data types in C are shown in the below table:

2. User-defined data types: These data types have been designed to ease the user's purpose. In C programming language, 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 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. Auto varibales are different from the constants in a C program.

The default contains a garbage value if an auto variable is not initialized. For every function call, the auto variable is recreated and re-initialized. It has local scope.

Register Storage Class

The keyword register is just a request to the compiler to allocate memory in the registers instead of RAM; if the accessible registers are available, then memory will be allocated in the registers. Otherwise, it will be allocated in the RAM only.

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.

Extern Storage Class

The variable declared extern would not be allocated any memory. These are also sometimes used in questions formation of GATE CSE exam. It will use the external memory (global variables).

Important Topics for Gate Exam
Responsivity Types Of Governors
SR Flip-FlopTypes Of Vibration
Norton's TheoremWelded Joints
P N Junction DiodeRound Robin Scheduling
Classless AddressingScaling In Computer Graphics

Comments

write a comment

FAQs on Variables in C

  • A variable in C is defined as the name given to a memory location where data values can be stored. A variable is declared along with a data type, that specifies the size and type of data value a variable can take. A variable can be of integer type, char type, float type, etc., depending on the declaration.

  • In C, we have four types of storage classes namely, auto, register, static, and extern which are associated with the lifetime, scope, storage and usage of a variable in C.

  • The compiler will throw a syntax error if a variable is not declared but used in the code/ program. It will show Lvalue or Rvalue error if a datatype is not declared.

  • Register variables are faster compared to auto variables as they reside in the CPU. Frequently used variables such as variables used in loops will give better performance if declared under the register storage class.

  • It is mandatory to declare a variable in C before using it. A variable in C is declared with the following syntax: data_type variable name;

    For example int length;

  • A Variable in C is used to store data of a specific type for later use in the program for execution by the user or programmer. For example int mark, float weight, etc. 

Follow us for latest updates