Open In App

Constants in C

Last Updated : 08 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C programming, constants are read-only values that cannot be modified during the execution of a program. These constants can be of various types, such as integer, floating-point, string, or character constants. They are initialized with the declaration and remain same till the end of the program.

Let's take a look at an example:

C
#include <stdio.h>

int main() {
 	
  	// Defining constant variable
  	const int a = 10;
 	printf("%d", a);
    return 0;
}

Output
10

Explanation: In the above program, we created a constant variable a with value 10. This value cannot be modified later in the program.

Syntax

We define a constant in C using the const keyword. Also known as a const type qualifier, the const keyword is placed at the start of the variable declaration to declare that variable as a constant.

const data_type var_name = value;

syntax-of-constants-in-c

Properties of Constant

The important properties of constant variables in C defined using the const keyword are as follows:

1. Initialization with Declaration

We can only initialize the constant variable in C at the time of its declaration. If we do not initialize it at the time of declaration, it will store the garbage value that was previously stored in the same memory.

C
#include <stdio.h>

int main() {
  
  	// Not initializing a constant variable
	const int a;
  
  	// printing value
  	printf("%d", a);
    return 0;
}

Output
0

The garbage value here is 0 but i can by anything depending on your environment.

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.

C
#include <stdio.h>
int main() {
  
    // Declaring a constant variable
    const int a;
  
    // Initializing constant variable var after declaration
    a = 20;
    printf("%d", a);
    return 0;
}


Output

In function 'main':
10:9: error: assignment of read-only variable 'a'
10 | a = 20;
| ^

Constants Using #define

In C, the #define directive can also be used to define symbolic constants that do not require a data type. They are called macros and are replaced by their values at compile time.

Syntax:

#define CONSTANT_NAME value

Example:

C
#include <stdio.h>
#define PI 3.14

int main() {
    printf("%.2f", PI);  
    return 0;
}

Output
3.14

Explanation: In this example, the value 3.14 is replaced by PI during the compilation.

Difference Between Constants and Literals

The constant and literals are often confused as the same. But in C language, they are different entities and have different semantics. The following table lists the differences between the constants and literals in C:

Constant

Literals

Constants are variables that cannot be modified once declared.Literals are the fixed values that define themselves.
Constants are defined by using the const keyword in C. They store literal values in themselves.They themselves are the values that are assigned to the variables or constants.
We can determine the address of constants.We cannot determine the address of a literal except string literal.
They are lvalues.They are rvalues.
Example: const int c = 20.Example: 24,15.5, 'a', "Geeks", etc.

const vs #define

The following table list the differences between the constants defined using const qualifier and #define in C:

Constants using constConstants using #define
They are the variables that are immutableThey are the macros that are replaced by their value.
They are handled by the compiler.They are handled by the preprocessor.
Syntax: const type name = value;Syntax: #define name value

Next Article
Article Tags :
Practice Tags :

Similar Reads