C Constants

Summary: in this tutorial, you’ll learn about C constants including literal and symbolic constants. Also, you’ll learn how to define symbolic constants in C.

C ConstantsIntroduction to C constants

Different from a variable, a constant is a special variable whose value does not change.

C supports two types of constants:

  • Literal constants
  • 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:

int x = 10; char c = 'A';
Code language: C++ (cpp)

In this example, the number 10 and the character ‘A’ are literal constants.

Symbolic constants

A symbolic constant is a constant that has a name. An example of a symbolic constant is EOF which stands for end-of-file.

Define symbolic constants

C provides several ways to define symbolic constants.

Define a symbolic constants using the define preprocessor

Sometimes, you want to use a constant in your code. For example, to calculate the area of a circle, you can do it as follows:

circle_area = 3.14159 * radius * radius;
Code language: C++ (cpp)

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

circle_area = PI * radius * radius;
Code language: C++ (cpp)

The PI symbolic constant makes your code more readable because it tells you not only the value but also its meaning.

Also, if a constant is used in several places, it’s better to use a symbolic constant because when you want to change its value, you just have to change in one place.

The following example calcualte the gross price from the net price and sale tax:

double gross_price = net_price + SALES_TAX * net_price;
Code language: C++ (cpp)

Later on, if the sales tax changes, you only have to change it in the place where it’s defined.

To define a symbolic constant, you can use C preprocessor as follows:

#define CONSTANT_NAME value
Code language: C++ (cpp)

For example, we can define PIand SALES_TAX symbolic constants as follows:

#define PI 3.14159 #define SALES_TAX 0.1
Code language: C++ (cpp)

By doing this, when the compiler compiles the program, it substitudes the PI and SALES_TAX with 1.14159 and 0.1.

By convention, the name of constants are uppercase to make it stand out in the code.

Define symbolic constants using const keyword

Since C-90, you can create a symbolic constant using the constkeyword. This approach is more flexible than using #define preprocessor.

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

const double PI = 3.14159; const double SALES_TAX = 0.1;
Code language: C++ (cpp)

Unlike the #define preprocessor, the constant defined with const keyword is typed. Also, the constant is scoped to the block where you define it. Note that you’ll learn more about the variable scope in the later tutorial.

Examples of using C constants

The following program illustrates how to use constants:

#include <stdio.h> #include <stdlib.h> #define PI 3.14159 int main() { /* calculate the area of the circle by its radius and PI */ int radius = 10; float circle_area = PI * radius * radius; printf("The area of the circle is %f\n", circle_area); /* calculate the gross price based on the net price and sales tax */ const float SALES_TAX = 0.1; float net_price = 100; float gross_price = net_price + net_price * SALES_TAX; printf("The gross price is %f\n", gross_price); return 0; }
Code language: C++ (cpp)

Summary

  • A constant stores a value that cannot be changed.
  • C supports both literal and symbolic constants.
  • Use the define preprocessor or const keyword to define a symbolic constant.
Was this tutorial helpful ?