Variables in C Language

In this tutorial, we will learn about variables in C language. What are the naming rules in the C language? Some special points for variables in the C language.

Variables are the containers used to store the value in our program. Variable is a named memory location where we can store and manage the values of our program.

A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in this program. The rules for naming variables are the same as those for naming identifiers. All the variables are stored temporarily in primary memory called RAM. Due to this, they are automatically deleted when function/program execution is completed.

Also See:- Identifiers and Data Types Quiz in C

Naming rules for variables in C language

There are some restrictions on the name of variables and symbolic constants. Variables and Constants are the basic data objects manipulated in a program.
The declaration lists the variables to be used, and states what type they have and perhaps what their initial values are.

  • Names are made up of letters and digits. 
  • The first character must be a letter.
  • The underscore “_” counts as a letter, But don’t begin variable names with an underscore. 
  • The Uppercase and lower case are distinct, so v and are two different names. 
  • Traditional C practice is to use lowercase for variable names, and all Uppercase for symbolic constants. 
  • Keywords like if, else, int, float, etc. are reserved, we can’t use them as a variable name. 

At least the first 31 characters of a local variable name are significant. For function names and external variables, the number may be less than 31. For external names, the standard guarantees uniqueness only for 6 characters and a single case.

int a; // variable declaration, variable a contains garbage value
a = 10; // initialization, variable b holds value 10
int b = 20; // variable defining
When we declare and initialize a variable at a time it is called variable defining.

int main()
   int idno = 5298, children=1;
   char name[20]="Rocco";
   char gender = 'M';
   float height = 5.7;

   printf("ID No = %d\n",idno);
   printf("\nName = %s\n",name);
   printf("\nGender = %c\n",gender);
   printf("\nheight = %.2f\n",height);
   printf("\nChildren = %d",children);

   return 0;


ID No = 5298
Name = Rocco
Gender = M
height = 5.70
Children = 1

Some special Important points on variables in C language

1. A variable must be declared before they are used in the program. It is a good idea to declare the variables at the first line of function. If we don’t initialize a variable then by default it will hold a garbage value.

2. Variables are case sensitive i.e. lower and upper case letters are different.

int main()
   int a ;
   int A ;
   a = 10;
   A = 20;
   return 0;


10 20

3. A variable can be of any of the data types, such as char, short, int, float, or double. But a variable can’t be of type void.

int main()
   int n1 = 10; 
   float n2 = 20.5;
   char ch = 'A';
   return 0;



4. Multiple variables of the same data type can be declared in multiple lines or only in one line.

int a;
int b;
int c;

Above three lines can be written in only one line as,

 int a, b, c;
int main()
   int a,b,c;
   a=20, b=30;
   printf("%d, %d, %d",a,b,c);
   return 0;


20, 30, 50

5. In an assignment statement, the right expression to the assignment operator (=) is evaluated first and that obtained result is stored in the left side variable to the assignment operator.

  • int a = 10; // variable a holds value 10
  • int b = 20; // variable b holds value 20
  • int c = a+b; // variable c holds value 30 because 10+20=30

6. C does not allow any expression, constant, or function to be placed to the left of the assignment operator. Thus its left operand should be a variable and its right operand may be an arbitrary expression.

  • x + y = z; // invalid
  • 10 = a; // invalid
  • add() = n; // invalid

Types of variables

We are using basically two types of variables in C language,

  1. Local variables
  2. Global variables

Local variables are accessible only inside the function or block. A global variable can be accessed anywhere within the program.

int a = 10;
int main()
     int a = 30;
   return 0;


a=10 a=30

Local and Global variables will be discussed later in detail after the function.

Look at this Program to convert Fahrenheit to Celsius from Fahrenheit = 0, 30, 60 to 500

#define LOWER 0
#define UPPER 500
int step ;
int main()
     int fahr;
     step=30 ;
     for(fahr=LOWER ; fahr <= UPPER ; fahr = fahr + step)
         printf("%d \t %6.2f \n", fahr, (5.0/9.0) * (fahr - 32));
     return 0;

Notice that there is no semicolon at the end of a #define line. Here LOWER and UPPER are symbolic constant names, fahr is a local or internal variable name (Within a function) and the step is an external variable name (Outside of all of the functions).

Symbolic constants names are conventionally written in uppercase so they can be readily distinguished from lower case variable names. 

It is wise to choose variable names that are related to the purpose of the variable. We tend to use short names for local variables, especially loop indices, and long names for external variables.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *