Defining Constants in C

Unlike a variable, a constant represents a value that never changes during the execution of a program.

In this lesson, we'll learn how to define constants in the C language and associate them with symbolic names.

Constants

Often, when writing programs, there is a need to introduce values that never change during the execution of the program.

For example, suppose we want to create a program that calculates the area and perimeter of a circle from a radius entered by the user. We can write the program like this:

#include <stdio.h>

int main() {
    /* Declarations */
    double radius, area, perimeter;

    /* Input */
    printf("Enter the radius of the circle: ");
    scanf("%lf", &radius);

    /* Area and perimeter calculation */
    area = 3.14159 * radius * radius;
    perimeter = 2 * 3.14159 * radius;

    /* Output */
    printf("The area of the circle is: %lf\n", area);
    printf("The perimeter of the circle is: %lf\n", perimeter);

    return 0;
}

In this program, to calculate the area and perimeter, we directly used the value of π (pi): 3.14159. Not only that, but we also repeated the same value twice.

In this case, the value of pi is a fixed number that never changes. In such cases, the C language provides a feature for defining constants to which symbolic names can be assigned.

This has two advantages:

  1. The code becomes more readable, since we can use a symbolic name that describes the meaning instead of a numerical value;
  2. We don't have to repeat the numerical value every time we need it.

The syntax to define a constant in C is as follows:

#define CONSTANT_NAME value

This is, in fact, a preprocessor directive that tells the compiler to replace every occurrence of CONSTANT_NAME with value before compiling the code. Specifically, it is a simple macro definition that we will study in more detail later. For now, we'll just use this mechanism to define constants.

Therefore, we can modify the code from above like this:

#include <stdio.h>

#define PI 3.14159

int main() {
    /* Declarations */
    double radius, area, perimeter;

    /* Input */
    printf("Enter the radius of the circle: ");
    scanf("%lf", &radius);

    /* Area and perimeter calculation */
    area = PI * radius * radius;
    perimeter = 2 * PI * radius;

    /* Output */
    printf("The area of the circle is: %lf\n", area);
    printf("The perimeter of the circle is: %lf\n", perimeter);

    return 0;
}

We added the definition of a constant before the main body of our program. We also replaced all occurrences of 3.14159 with PI. The code becomes more readable, as the meaning of PI is immediately clear.

Definition

Defining Constants in the C Language

To define a constant in the C language, you can use the preprocessor directive #define. This directive allows you to associate a symbolic name with a constant value, making the code more readable and maintainable.

The general syntax to define a constant is:

#define CONSTANT_NAME value

Where CONSTANT_NAME is the symbolic name of the constant and value is the numerical value associated with the constant.

Hint

Constant Names

A widely adopted convention in writing C programs is to write constant names in uppercase. This makes it easier to distinguish them from variables and more visible within the code.

So, for example, it's recommended to write a constant like this:

/* Recommended */
#define INCHES_IN_A_CENTIMETER 2.54

Instead of like this:

/* Not recommended */
#define inches_in_a_centimeter 2.54

Example

Let's look at another example of using constants. Suppose we want to create a program that takes a temperature in degrees Celsius as input and converts it to degrees Fahrenheit. The conversion formula is:

F = \frac{9}{5} \cdot C + 32 = 1.8 \cdot C + 32

Where F is the temperature in degrees Fahrenheit and C is the temperature in degrees Celsius.

We can think of defining two constants: one for the conversion coefficient and one for the additive term. We can then write the program as follows:

#include <stdio.h>

/* 9 / 5 = 1.8 */
#define COEFFICIENT 1.8
#define CONSTANT_TERM 32.0

int main() {
    /* Declarations */
    double celsius, fahrenheit;

    /* Input */
    printf("Enter the temperature in degrees Celsius: ");
    scanf("%lf", &celsius);

    /* Conversion */
    fahrenheit = COEFFICIENT * celsius + CONSTANT_TERM;

    /* Output */
    printf("The temperature in degrees Fahrenheit is: %lf\n", fahrenheit);

    return 0;
}

Running this program will produce output similar to the following:

Enter the temperature in degrees Celsius: 25
The temperature in degrees Fahrenheit is: 77.000000

In Summary

The essence of this lesson is the following:

  • Constants are values that do not change during the execution of a program;
  • To define constants in the C language, the preprocessor directive #define is used;
  • The general syntax to define a constant is #define CONSTANT_NAME value;
  • It is recommended to write constant names in uppercase to distinguish them easily from variables.

Constants are a very useful tool for making code more readable and maintainable. It's advisable to use them whenever fixed values need to be introduced in a program.

In the next lesson, we will study an important topic in C programming: identifiers.