C Constants

Summary: this tutorial introduces you to C constants including literal constants and symbolic constants. We will show you two ways to define symbolic constants in C.

C ConstantsIntroduction to C constants

Different from a variable, a constant is a special variable whose value is unchanged during the program execution.

C has two types of constants: literal constants and symbolic constants. Each type of constant has its own uses.

Literal constants

A literal constant is a value that you put directly in your code for example:

In this example 10 and ‘A’ are literal constants.

Symbolic constants

A symbolic constant is a constant that has a name. C provides several ways to define symbolic constants.

Symbolic constants using C preprocessor

There are a lot of cases that you want to use a constant in your code, for example to calculate the square of a circle, you often do it as follows:

In the statement, the number 3.14159 is the value of PI. It is a literal constant. Instead of using a literal constant, you can use a symbolic constant as follows:

The PI symbolic constant makes your code more readable because it tells you not only the value but also its meaning. In addition, if a constant is used in several places, it is better to use a symbolic constant because when you want to change its value, you just have to change in one place where the symbolic constant is defined. See the following example:

Later on, if the tax rate changes, you only have to change it in the place where the TAXRATE symbolic constant is defined.

How do you define symbolic constants in C? The first idea of defining a C constant is using preprocessor as follows:

For example, we can define PI and TAXRATE symbolic constants as follows:

By doing this, when the program is compiled, the constant values 1.14159 and 0.1 will be substituted everywhere you used PI and TAXRATE. This feature is known as compile time substitution. Those symbolic constants defined using the #define preprocessor are also called manifest constants because when you run the program, all the substitutions have already been made.

Note that there is no assignment operator ( =) between constant’s name and value, and there is no semicolon ( ;) at the end of the statement.

By convention, the name of constants are in upper case so when you see them in the code you know that they are constants.

Symbolic constants using const keyword

C-90 officially added a second way to create a symbolic constant using the const keyword. This approach is more flexible than using #define preprocessor e.g., you can use the const keyword to declare a constant pointer.

Once the value of a symbolic constant using const keyword has been initialized, its value cannot be changed throughout the program. See the following example:

Examples of using C constants

In this example, we are using two ways of defining symbolic constants including preprocessor and const keyword to calculate the square of a circle and total value of a product based on a predefined tax rate. The following is the demo program that illustrates how to use the C constants:

In this tutorial, you’ve learned various ways to define C constants using the #define preprocessor and the const keyword.

  • Was this tutorial helpful ?
  • YesNo