A constant is a value that is not intended to be changed during the execution
of a program. C provides several ways to work with values that should remain
fixed, including the const qualifier and preprocessor macros using
#define.
const-qualified object cannot be modified
through that object. Constants are useful for values such as fixed limits,
mathematical values, and configuration values.
A constant is a value that is intended to remain unchanged while the program is running.
const int MAX_MARKS = 100;
Here, MAX_MARKS is declared as a constant integer.
Constants are useful because they:
The const keyword can be used when declaring an object whose value
should not be modified through that object.
const int age = 20;
The variable age cannot be assigned a new value through this
declaration.
An integer constant can be declared using const int.
const int DAYS = 7;
The value of DAYS is initialized to 7.
A floating-point object can also be declared with const.
const float PI = 3.14f;
The value is intended to remain unchanged through the object
PI.
The double type can also be used with const.
const double TAX_RATE = 0.18;
This can be useful when a fixed value requires floating-point precision.
A character can also be declared as a constant.
const char GRADE = 'A';
The character value should not be changed through the object
GRADE.
For a const object with automatic storage duration, initialization
should be provided when it is declared because its value cannot later be
assigned in the normal way.
const int MAX = 100;
A program should not attempt to assign a new value to a
const-qualified object.
const int age = 20;
// age = 25; // Not allowed
The assignment is invalid because age is const-qualified.
There is no requirement that const variable names be uppercase.
However, uppercase names are commonly used by programmers to make fixed
values easy to recognize.
const int MAX_SIZE = 100;
const float PI = 3.14f;
The preprocessor directive #define can be used to create a
macro representing a value.
#define MAX_MARKS 100
The preprocessor replaces occurrences of the macro name according to the macro definition before compilation.
#include <stdio.h>
#define MAX_MARKS 100
int main()
{
printf("%d", MAX_MARKS);
return 0;
}
The output is:
100
A preprocessor macro definition normally does not end with a semicolon.
#define PI 3.14
Do not write:
#define PI 3.14;
when you intend PI to expand to the numeric value
3.14.
| const | #define |
|---|---|
| Declares an object with a type | Creates a preprocessor macro |
| Handled by the compiler | Handled during preprocessing |
| Follows normal C type rules | Performs textual macro replacement |
const int MAX = 100; |
#define MAX 100 |
Numbers written directly in a C program are integer constants when they represent integer values.
10
100
-25
0
These values can be used in expressions and assignments.
Values containing a decimal point are commonly written as floating-point constants.
10.5
3.14
99.99
The compiler determines their type according to C's literal rules.
A character constant is normally written inside single quotation marks.
'A'
'B'
'7'
These are character constants, not string literals.
Text written inside double quotation marks is a string literal.
"Hello"
"Welcome to C"
"Programming"
A string literal can contain multiple characters.
Escape sequences represent special characters inside character and string literals.
\n
\t
\\
\"
For example, \n represents a newline character.
#include <stdio.h>
int main()
{
const int PRICE = 100;
int quantity = 3;
int total = PRICE * quantity;
printf("Total = %d", total);
return 0;
}
Here, the constant PRICE is used in a calculation.
#include <stdio.h>
int main()
{
const int MAX_MARKS = 100;
printf("Maximum Marks = %d", MAX_MARKS);
return 0;
}
#include <stdio.h>
int main()
{
const int DAYS_IN_WEEK = 7;
printf("Days = %d", DAYS_IN_WEEK);
return 0;
}
#include <stdio.h>
int main()
{
const double TAX_RATE = 0.18;
printf("Tax Rate = %.2f", TAX_RATE);
return 0;
}
A program can contain multiple constants.
const int DAYS = 7;
const int MONTHS = 12;
const double PI = 3.14159;
Each constant can represent a different fixed value.
#include <stdio.h>
int main()
{
const int PASS_MARKS = 40;
int marks = 75;
if (marks >= PASS_MARKS)
{
printf("Pass");
}
return 0;
}
Using a named constant makes the condition easier to understand.
Compare these two expressions:
if (marks >= 40)
and:
const int PASS_MARKS = 40;
if (marks >= PASS_MARKS)
The named constant clearly explains what the value 40 represents.
const object.#define macro.#include <stdio.h>
int main()
{
const int DAYS = 7;
const float PI = 3.14f;
const char GRADE = 'A';
printf("Days = %d\n", DAYS);
printf("PI = %.2f\n", PI);
printf("Grade = %c\n", GRADE);
return 0;
}
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
const int MIN_SIZE = 10;
printf("Minimum = %d\n", MIN_SIZE);
printf("Maximum = %d\n", MAX_SIZE);
return 0;
}
Both const objects and preprocessor macros can represent fixed
values, but they work differently.
#include <stdio.h>
#define MAX_MARKS 100
int main()
{
const int PASS_MARKS = 40;
int marks = 85;
printf("Maximum Marks = %d\n", MAX_MARKS);
printf("Passing Marks = %d\n", PASS_MARKS);
printf("Student Marks = %d\n", marks);
if (marks >= PASS_MARKS)
{
printf("Result: Pass");
}
return 0;
}
This example shows how named fixed values can make a C program easier to read and maintain.
const keyword can be used to declare a const-qualified object.#define creates a preprocessor macro.Question: Which keyword is used to declare a const-qualified object in C?