Javatpoint Logo
Javatpoint Logo

Difference Between Variables and Constants

What is a variable?

A variable is like a container containing the values or data of a particular data type that helps to execute the program. The value of a variable can change depending on the conditions or information given at the program's run time. A variable can be defined in both ways, like the uppercase and lowercase letters, digits and the underscores. A variable can store the value of the different data types like integer, float, character, enum.

Rules for defining the variable name in C programming language:

  1. The variables are case sensitive.
  2. The variable name must start with an alphabet letter or underscore.
  3. It includes the letter, digits and underscore.
  4. There should not be a white space in a variable name.
  5. The name of a variable should not be any reserved keywords like int, float, str, char, etc.

Declaration of Variables

It is simple to declare the variables in the C program.

Initialization of Variables

It is the simple initialization of variable in C program:


A constant is a fixed value whose value cannot be changed during program's execution or once the value is defined. It is also known as literal. For example, 5, 20, 'a', 'Radius', 5.2, "Welcome back", etc. A constant can be defined in two ways, like #define pre-processor and by const keyword. The constants can be different data types, such as integer constants, floating constants, character constants, string constants and enumeration constants. Let's understand briefly about them:

  1. Integer constant
    An integer constant is a whole number and can be large without including any decimal points. For example, 0, 1, 2, 123, 5767, 05, 0X23, 0xFFF, etc.
  2. Float constant
    The float constants are the part of an integer constant that containing a decimal point, fractional form and exponential form.
    Here are some example of floating point constants:
    0.5, 35.05, 2.3e6, 3.52f or 3.52F, PI = 3.14, etc.
  3. Character Constants
    It is a single character constant enclosed within a single quotation mark (like 'a', 'A'), called a character constants. There are some valid constants as: 'g', 'D', ' ', '#'.
  4. String Constant
    It is the character set of string constants that are enclosed in a double quote. The character may be letters, numbers, special symbols and some blank space. Furthermore, a string constant contains zero, one or more continuous character in double quotation marks. For example, "Hello Friends", "Computer", "5987", " " , "A".

Note: "A" and 'A' are different; the first one is a string constant consisting of character A and \0. While the second 'A' represents is a character constant whose integer value is 65.

Example 1: Let's create a program to use the #define pre-processor in C constants.


Variables vs Constants

Example 2: Write create a program to display the use of const keyword in C.


Variables vs Constants

Difference between Variables and Constant in C Program

Variables Constants
It is a variable that stores data type value in a program. It is similar to a variable and cannot be changed during program execution.
It is a variable that can be changed after defining the variable in a program It is a fixed variable that cannot be changed after defining the variable in a program.
The value of a variable can change depending on the conditions. In constants, the value cannot be changed.
Typically, it uses int, float, char, string, double, etc. data types in a program. It can be express in two ways: #define pre-processor and the const keyword.
Example: int a = 5; float radius = 5.2; char 'A'; Example: const int Len = 5;
#define PI 3.14

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA