A constant is a literal value or a named value defined using the const or #define keyword.
Unlike variables, the value of a constant cannot be modified once it is defined.
Example:
const int age = 18;
age = 20; // ❌ Error: assignment of read-only variable
Types of Constants in C
C supports the following kinds of constants:
- Integer Constants
- Real (Floating-Point) Constants
- Character Constants
- String Constants
- Enumeration Constants
- Constants Using
constKeyword - Constants Using
#define
Let’s understand them in detail.
1. Integer Constants
These represent whole numbers without any fractional part.
Valid Examples:
10
-50
0
2000
Invalid:
10.5 // Contains decimal
1,000 // Comma not allowed
Integer Constants Types:
- Decimal (base 10):
10,245 - Octal (base 8): Must start with 0 →
015 - Hexadecimal (base 16): Must start with 0x →
0xAF
int a = 15; // decimal
int b = 015; // octal (13 in decimal)
int c = 0xAF; // hexadecimal (175 in decimal)
2. Real (Floating-Point) Constants
These represent numbers with decimal points or scientific notation.
Examples:
10.5
0.001
-25.75
6.02e23
float price = 99.99;
double gravity = 9.81;
3. Character Constants
A character constant is a single character enclosed in single quotes (‘ ‘).
Examples:
'A'
'9'
'$'
'a'
Every character constant has an ASCII value.
printf("%d", 'A'); // Output: 65
4. String Constants
A sequence of characters enclosed in double quotes (” “).
Examples:
"Hello"
"C Programming"
"1234"
"@#&$"
Strings end with a special null character \0 automatically.
char message[] = "Welcome to C";
5. Escape Sequence Constants
These are special characters that represent actions such as newline or tab, starting with backslash \.
| Escape Sequence | Meaning |
|---|---|
\n | New line |
\t | Horizontal tab |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
\0 | Null character |
printf("Hello\nWorld");
6. Enumeration Constants (enum)
Used to define a set of named integer constants.
enum week { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
Here, Mon = 0, Tue = 1, etc., unless manually assigned.
7. Constants Using const Keyword
The const keyword makes variables read-only.
const float PI = 3.14159;
const int MAX = 100;
These constants have fixed values that cannot change.
8. Constants Using #define Macro
The #define preprocessor directive creates symbolic constants.
#define PI 3.14159
#define MAX 100
Unlike const, this is replaced during preprocessing (before compilation).
const vs #define (Comparison)
| Feature | const | #define |
|---|---|---|
| Checked by compiler | Yes | No |
| Has data type | Yes | No |
| Stored in memory | Yes | No (text replacement) |
| Debugging support | Strong | Weak |
| Example | const int a=10; | #define a 10 |
Advantages of Using Constants
- Prevent accidental value changes
- Improve readability
- Easier to modify (change value once → updates everywhere)
- Improve maintainability
- Reduce hard-coded values in code
Example Program Using Different Constants
#include <stdio.h>
#define MIN_AGE 18
int main() {
const float PI = 3.14;
char grade = 'A';
char name[] = "Sagar";
printf("PI: %.2f\n", PI);
printf("Name: %s\n", name);
printf("Grade: %c\n", grade);
printf("Minimum Age: %d\n", MIN_AGE);
return 0;
}
Conclusion
Constants are critical in C programming because they ensure that certain values remain fixed throughout the program. From numbers to strings and characters to macros, constants help create stable and maintainable applications.
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming