Variable in C Language - Use, Data Type & Syntax

By Mona Kumari|Updated : May 9th, 2022

A variable in C is just 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.

In the article, we will dig deep into what variable in C 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 about 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.

Table of Content

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
  1. 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

FAQs on Variable in C

  • 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. 

  • 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;

Follow us for latest updates