Constants in C
Constants in C
Constants in C
Last Updated : 08 Jan, 2024
The constants in C are the read-only variables whose values cannot be modified once they are
declared in the C program. The type of constant can be an integer constant, a floating pointer
constant, a string constant, or a character constant. In C language, the const keyword is used to
define the constants.
In this article, we will discuss about the constants in C programming, ways to define constants in
C, types of constants in C, their properties and the difference between literals and constants.
What is a constant in C?
As the name suggests, a constant in C is a variable that cannot be modified once it is declared in
the program. We can not make any change in the value of the constant variables after they are
defined.
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 1/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
Example of Constants in C
int main()
{
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 2/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
return 0;
}
Output
One thing to note here is that we have to initialize the constant variables at declaration.
Otherwise, the variable will store some garbage value and we won’t be able to change it. The
following image describes examples of incorrect and correct variable definitions.
Types of Constants in C
The type of the constant is the same as the data type of the variables. Following is the list of the
types of constants
Integer Constant
Character Constant
Floating Point Constant
Double Precision Floating Point Constant
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 3/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
Array Constant
Structure Constant
We just have to add the const keyword at the start of the variable declaration.
Properties of Constant in C
The important properties of constant variables in C defined using the const keyword are as
follows:
We can only initialize the constant variable in C at the time of its declaration. Otherwise, it will
store the garbage value.
2. Immutability
The constant variables in c are immutable after its definition, i.e., they can be initialized only
once in the whole program. After that, we cannot modify the value stored inside that variable.
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 4/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
int main()
{
// declaring a constant variable
const int var;
// initializing constant variable var after declaration
var = 20;
Output
In function 'main':
10:9: error: assignment of read-only variable 'var'
10 | var = 20;
| ^
Constant Literals
Constants are variables that cannot be modified Literals are the fixed values that define
once declared. themselves.
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 5/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
Constant Literals
int main()
{
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 6/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
Output
Note: This method for defining constant is not preferred as it may introduce bugs and
make the code difficult to maintain.
FAQs on C Constants
Answer:
Constants in C are the immutable variables whose values cannot be modified once they
are declared in the C program.
Answer:
The const keyword is the qualifier that is used to declare the constant variable in C
language.
Answer:
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 7/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
Answer:
The right way to declare a constant in C is to always initialize the constant variable when
we declare.
Q5. What is the difference between constant defined using const qualifier and #define?
Answer:
The following table list the differences between the constants defined using const qualifier
and #define in C:
They are handled by the compiler. They are handled by the preprocessor.
Answer:
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 8/9
5/12/24, 9:50 PM Constants in C - GeeksforGeeks
Yes, we can take advantage of the loophole created by pointers to change the value of a
variable declared as a constant in C. The below C program demonstrates how to do it.
int main()
{
https://www.geeksforgeeks.org/constants-in-c/?ref=next_article 9/9