Variables in C Language

A variable in any programming language is a place holder for data used in the program. When data is to be used in the program, we store them in some memory location and name that location for easier access.

The naming of addresses is known as variables. Variable is the name of the memory location. Unlike constant, the value of the variable can be changed during the execution of the program.

Each variable has a data type associated with it which tells about the type of value a variable can store along with memory alignment and range of values.

Common data types supported in C are:

  • int: used to hold integer
  • char: used to hold a character
  • float: used to hold decimal values
  • double: Used to hold double values
  • void

Foe details please refer Data type in C

Rules for naming a variable:

There are some protocols that need to be followed while naming a variable which are as follows:

  • The variable cannot begin with digits.
  • It can consist of alphabet, digits and special symbols like underscore.
  • Blanks and spaces are not allowed in variables.
  • Keywords cannot be used as a variable name.
  • Variables are case-sensitive.

Declaring, defining and initializing a variable

Declaring a variable:

Declarations of variables must be done before they are used in the program. It says the following thing:

  • It tells the compiler the name of the variable
  • It specifies what type of value the variable can hold.
  • Unless defined, the compiler does not allocate memory for the variable.
  • It’s like prior informing the compiler about the name and type of variable.
  • A variable is declared using the extern keyword, outside the main function.
  • The structure is defined only when we create an object for that.


extern int a;
struct student
   int roll_no;
   char name[10];

Defining a variable:

Defining a variable means the compiler has allocated memory and can be used in the program. It is not necessary to declare a variable using an extern keyword, it can directly be defined as a variable inside the main function.

The structure is defined upon the creation of the object for the same.

int a;
int c, d;
struct student
  int rol_no;
  char name[10];
int main()
   struct student student1;
   int count;

Initializing a variable:

Assigning a value to a variable is called the initialization of a variable. Variables can be defined and initialized in a single statement.

int a = 10;

Sample program:

#include <stdio.h>
/* Variable declaration, this is optional *.
extern int a, b;
int main()
   /* Variable Definition */
   int a;
   int b;

   /* variable Initialization */
   a = 10;
   b = 20;

   printf ("\n The sum is 5d\n", a + b);
   return 0;

Relevant Posts:

Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: