Lesson 10 of 60 – Constants in C
17%

Constants in C

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.

Note: A const-qualified object cannot be modified through that object. Constants are useful for values such as fixed limits, mathematical values, and configuration values.

1. What is a Constant?

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.

2. Why Use Constants?

Constants are useful because they:

  • Prevent accidental changes to fixed values.
  • Make programs easier to understand.
  • Make repeated values easier to manage.
  • Improve code readability.
  • Make changes easier when a fixed value needs updating.

3. Using the const Keyword

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.

4. const Integer

An integer constant can be declared using const int.

const int DAYS = 7;

The value of DAYS is initialized to 7.

5. const Float

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.

6. const Double

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.

7. const Character

A character can also be declared as a constant.

const char GRADE = 'A';

The character value should not be changed through the object GRADE.

8. Constant Must Be Initialized

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;

9. Changing a const Variable

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.

10. Constant Names

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;

11. Using #define

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.

12. #define Example

#include <stdio.h>

#define MAX_MARKS 100

int main()
{
    printf("%d", MAX_MARKS);

    return 0;
}

The output is:

100

13. #define Does Not Use a Semicolon

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.

14. const vs #define

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

15. Integer Constant Literals

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.

16. Floating-Point Constants

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.

17. Character Constants

A character constant is normally written inside single quotation marks.

'A'
'B'
'7'

These are character constants, not string literals.

18. String Literals

Text written inside double quotation marks is a string literal.

"Hello"
"Welcome to C"
"Programming"

A string literal can contain multiple characters.

19. Escape Sequence Constants

Escape sequences represent special characters inside character and string literals.

\n
\t
\\
\"

For example, \n represents a newline character.

20. Using a Constant in a Calculation

#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.

21. Constant for Maximum Marks

#include <stdio.h>

int main()
{
    const int MAX_MARKS = 100;

    printf("Maximum Marks = %d", MAX_MARKS);

    return 0;
}

22. Constant for Days in a Week

#include <stdio.h>

int main()
{
    const int DAYS_IN_WEEK = 7;

    printf("Days = %d", DAYS_IN_WEEK);

    return 0;
}

23. Constant for Tax Rate

#include <stdio.h>

int main()
{
    const double TAX_RATE = 0.18;

    printf("Tax Rate = %.2f", TAX_RATE);

    return 0;
}

24. Multiple Constants

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.

25. Constants in Conditions

#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.

26. Constants Improve Readability

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.

27. Common Mistakes with Constants

  • Trying to assign a new value to a const object.
  • Confusing character constants with string literals.
  • Adding an unwanted semicolon to a simple #define macro.
  • Using unclear names for important fixed values.
  • Changing repeated literal values separately throughout a program.

28. const Example with Different Types

#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;
}

29. const and #define Example

#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.

30. Complete Constants Example

#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.

📌 Key Points

  • A constant represents a value intended to remain fixed.
  • The const keyword can be used to declare a const-qualified object.
  • #define creates a preprocessor macro.
  • Const-qualified objects have a data type.
  • Macro replacement happens during preprocessing.
  • Integer, floating-point, character, and string literals are common kinds of constants or literal values in C.
  • Meaningful constant names improve readability.
  • Constants are useful for fixed values such as limits and rates.

🧠 Quick Quiz

Question: Which keyword is used to declare a const-qualified object in C?