Constants in C

The constants are those variables or values in the C which cannot be modified once they are defined in the program.

  • They have fixed values till the program’s life.
  • We can only assign value to the constant in the declaration.
  • There can be any type of constant like integer, float, octal, hexadecimal, character constants, etc.

Example of C Constant

C




#include <stdio.h>
 
// Constants Macro
#define val 10
 
// Driver code
int main()
{
    // constant variables
    const float floatVal = 5.8;
    const char charVal = 'a';
 
    // printing constants
    printf("Integer Constant: %d\n", val);
    printf("Floating point Constant: %f\n", floatVal);
    printf("Character Constant: %c\n", charVal);
 
    return 0;
}


Output

Integer Constant: 10
Floating point Constant: 5.800000
Character Constant: a

Difference Between Constants and Variables in C

The constants and variables in C are both used to store data. So it is essential to know the difference between the variables and constants in C so that we can decide which one to use based on the situation.

In this article, we will discuss the basic difference between a constant and a variable in C language.

Similar Reads

Variables in C

A variable in simple terms is a storage place that has some memory allocated to it. It is used to store some form of data and retrieve it when required. Different types of variables require different amounts of memory and have some specific set of operations that can be applied to them....

Constants in C

...

Difference between Constant and Variables

The constants are those variables or values in the C which cannot be modified once they are defined in the program....