In C programming, a constant is a value that, once defined, cannot be altered or modified during the execution of a program. Constants represent fixed values that are used in computations or logic but remain unchanged throughout the program’s lifetime.
Constants can be either numeric, character, or string values and can be defined in various ways depending on the programming language.
Types of Constants in C
1. Symbolic Constants:
These are constants that are assigned a name, making the code more readable and maintainable. They can be defined using two types.
a. #define Directive
The #define directive in C is part of the preprocessor phase, which occurs before the program is compiled. It creates symbolic constants by replacing a name with a value throughout the code wherever that name is used. It’s important to note that #define does not create variables, but rather simple text replacements.
Syntax:
#define CONSTANT_NAME value
- CONSTANT_NAME is the name of the constant.
- value is the value assigned to that constant.
Example:
#include <stdio.h>
#define MAX_SPEED 180 // Define a constant for MAX_SPEED
#define MIN_SPEED 20 // Define a constant for MIN_SPEED
int main() {
printf("Car maximum speed : %d\n", MAX_SPEED);
printf("Car minimum speed : %d\n", MIN_SPEED);
return 0;
}
Output:
Car minimum speed : 20
b. const Keyword
The const keyword is used to define constants where the value is assigned at the time of declaration and cannot be modified later in the program.
Syntax:
const data_type CONSTANT_NAME = value;
Example:
#include
int main() {
const int MAX_SPEED = 180;
printf("Car's maximum speed: %d\n", MAX_SPEED);
return 0;
}
Output:
2. Literal Constants:
A literal constant (or simply literal) in C is a fixed value that is directly written in the program code. These constants are used as immediate values in expressions and do not have a name associated with them. The value of a literal constant is fixed and cannot be changed during the program’s execution.
Types of Literal Constants
1. Integer Literals: These represent integer values.
2. Floating-point Literals: These represent decimal or floating-point numbers.
3. Character Literals: These represent single characters.
4. String Literals: These represent a sequence of characters (strings).
Example:
#include <stdio.h>
int main() {
// Integer literal
int num = 10;
// Floating-point literal
float pi = 3.14159;
// Character literal
char grade = 'A';
// String literal
char message[] = "Hello, World!";
// Print all the literals
printf("Integer Literal: %d\n", num);
printf("Floating-Point Literal: %.5f\n", pi);
printf("Character Literal: %c\n", grade);
printf("String Literal: %s\n", message);
return 0;
}
Output:
Floating-Point Literal: 3.14159
Character Literal: A
String Literal: Hello, World!
Why Use Constants
1. Improved Code Readability and Clarity: Constants provide descriptive names for values used throughout your code, making it easier to understand what the values represent.
Example:
const int MAX_SPEED = 180;
2. Memory Efficiency: Constants like those declared with the const keyword are variables with memory allocation but are stored in read-only memory, reducing the risk of accidental modification. This can also optimize the program’s memory usage, as the value can be reused wherever needed.
3. Code Consistency: Using constants ensures that the same value is consistently used throughout the program. This reduces the risk of introducing inconsistencies when manually retyping the value in different parts of the code.
4. Prevention of Unintended Modifications: By using constants, you ensure that their values cannot be accidentally modified during the program’s execution. This helps prevent logic errors and bugs.