When you're programming, there are times when you'll want the values of certain variables to remain unchanged. In the C language, you'll likely define them as constants.

You can define constants in C in a few different ways. In this tutorial, you'll learn how to use #define and the const qualifier to define them.

Let's get started.

How to Use #define to Define Constants in C

One of the common ways to define constants in C is to use the #define preprocessor directive, as shown below:

#define <VAR_NAME> <VALUE>

In the above syntax:

  • <VAR_NAME> is a placeholder for the name of the constant.
  • It's recommended that you name constants in the uppercase, as it helps differentiate them from other variables defined in the program.
  • <VALUE> is a placeholder for the value that <VAR_NAME> takes.
  • #define is a preprocessor directive.
  • Before the compilation of the program, the preprocessor replaces all occurrences of <VAR_NAME> with <VALUE>.

In C, the preprocessors process the source code ahead of compilation to produce the expanded source code. This is illustrated below.

C Preprocessor

It's a good practice to include the definition of all constants after the header files in your program, as shown below:

#include <stdio.h>

#define CONSTANT_1 VALUE_1
#define CONSTANT_2 VALUE_2
//

int main()
    {
        //statements here
    }

▶ In the next section, you'll see an example using #define to declare C constants.

How to Declare Constants Using #define Example

Consider the following code snippet, where you have two constants STUDENT_ID and COURSE_CODE.

#include <stdio.h>
#define STUDENT_ID 27
#define COURSE_CODE 502

int main()
{
    printf("Student ID: %d is taking the class %d\n", STUDENT_ID,COURSE_CODE);

    return 0;
}


# Output
Student ID: 27 is taking the class 502

In this example:

  • The preprocessor replaces STUDENT_ID and COURSE_CODE by 27, and 502, respectively. So the body of the main() function will now be:
int main()
{
    printf("Student ID: %d is taking the class %d\n", 27, 502);

    return 0;
}
  • As the printf() function can print out formatted strings, the two occurrences of the format specifiers %d (for decimal integers) are replaced by 27 and 502.
Although #define lets you define constants, you should be careful not to redefine them elsewhere in the program.

For example, the code below, you've redefined STUDENT_ID. And it will compile and execute without errors.

#include <stdio.h>
#define STUDENT_ID 27
#define STUDENT_ID 207 //redefinition of a #define constant.
#define COURSE_CODE 502

int main()
{
    printf("Student ID: %d is taking the class %d\n", STUDENT_ID,COURSE_CODE);

    return 0;
}

Depending on your compiler, you may get a warning that you're trying to redefine an already-defined constant.

And the value in the most recent definition will be used.

Notice how the redefined value of 207 has been used as the STUDENT_ID, overwriting the previously defined value 27.

So you now know that the #define constants are in some sense not true constants as you can always redefine them.

Head on to the next section to learn about the const qualifier.

How to Use the const Qualifier to Define Constants in C

In C, <data_type> <var_name> = <value> is the syntax to declare a variable <var_name> of type <data_type>, and to assign it the value <value>.

To make <var_name> a constant, you only need to add the const qualifier to this statement as follows:

const <data_type> <var_name> = <value>;
Adding the const keyword in the definition of the variable ensures that its value remains unchanged in the program.

The const qualifier makes the variable read-only. And trying to modify it elsewhere in the program will throw errors during compilation.

▶ Head on to the next section to modify the previous example using const.

How to Declare Constants Using const Qualifier Example

From the previous example, you have the constants STUDENT_ID and COURSE_CODE. Now you'll define them as constants using the const qualifier.

  • Since they're both integers, you can define them to be of the int data type, taking the intended values: 27 and 502.
  • Include the qualifier const in the respective definitions.

This is shown in the code snippet below:

#include <stdio.h>

int main()
{
    const int STUDENT_ID = 27;
    const int COURSE_CODE = 502;
    printf("Student ID: %d is taking the class %d\n", STUDENT_ID, COURSE_CODE);

    return 0;
}

# Output
Student ID: 27 is taking the class 502

You can see that the code works as expected.

In C, you cannot redefine an existing variable.

For example, if int my_var = 2 is the first definition, your program won't compile successfully if you try redefining my_var as int my_var = 3.

However, you can always reassign values of a variable.

This means that if int my_var = 2 is the definition, you can assign a different value to my_var using a simple assignment statement like my_var = 3.

Let's now try modifying the const variable STUDENT_ID.

#include <stdio.h>

int main()
{
    const int STUDENT_ID = 27;
    STUDENT_ID = 207; // modifying a true constant: NOT POSSIBLE
    const int COURSE_CODE = 502;
    printf("Student ID: %d is taking the class %d\n", STUDENT_ID, COURSE_CODE);

    return 0;
}

You'll see that the program doesn't compile successfully.

And the error message reads: error: assignment of read-only variable 'student_id' meaning that you can only read the value of STUDENT_ID and not assign a value to it.

Thus the const qualifier renders a true constant that's immune to changes, and cannot be altered during the execution of the program.

Conclusion

In this tutorial, you've learned how to define constants:

  • using the #define preprocessor directive with the syntax #define <VAR_NAME> <VALUE>, and
  • using the const qualifier to render variables to be read-only.

Hope you found this tutorial helpful. Happy coding!😄