Constants and Variables are integral components of building a C Program. If we don't have a proper notion about them, continuous errors are what can be expected. In this article, we'll distinguish them and find out what is difference between Constants and Variables in C.

Constants and Variables in C
Constants and Variables in C

Constants in C

A constant in C refers to a fixed value that does not change during the execution of a program. C supports 2 types of constants:

1. Numeric Constants:

C supports two types of Numeric Constants. They are Integer Constants and Floating Point Constants.

2. Character Constants:

C supports two types of Character Constants. They are Single Character Constants and String Constants.

Examples of Integer Constants:

+25, -10, 33, etc.

Examples of Floating Point Constants:

0.23, -0.4777, etc.

Examples of Single Character Constants:

'+', '$', '*', etc.

Examples of String Constants:

"My Name is Sukalyan", "Sum = ", etc.

Variables in C

Variables are named location in memory that can be used to hold a value that may be modified by the program. The first letter of naming a variable must be an alphabet. Keywords can't be used to denote variable.

1. Variable Declaration:

Variable Declaration refers to that part of a program where a variable is introduced for the first time in a program, prior to its use. Typically, a variable is declared as below:

type variable_name;

or for multiple variables:

type variable1_name, variable2_name, variable3_name;

Here, any variable name that one programmer declares can only contain alphabets(irrespective of case), underscore (_) and alphabet followed by a number. But, variable name can't begin with a number alone.

2. Variable Definition:

Variable definition is that part of a C Program  where the variable is assigned a memory location and a value. Usually, variable declaration and definition are carried out together.

But, can't we separately declare and define a variable? Yes, we can. Using extern variable and creating functions anyone can do that. We'll get to know more about extern in subsequent articles. For now, take a look at the following program to understand variable declaration and definition:

#include <stdio.h> 
int main() 
    // declaration and definition of variable 'x' 
    char x = 'X';    
    // This is also both declaration and definition as 'y' is allocated 
    // memory and assigned some garbage value.    
    float y;     
    // multiple declarations and definitions 
    int _z, _w12, t;    
    // Let's print a variable 
    printf("%c \n", x);   
    return 0; 



Books I Prefer:


I hope you now have a clear idea about constants and variables in C Programming Language. Write down your opinions in comments below.