• Home
  • Constants in C/C++

Constants in C/C++

Constants are fixed values that are defined in a C or C++ program and cannot be changed during the execution of the program. There are several ways to define constants in C and C++:

#define preprocessor directive: The #define preprocessor directive is used to define constants in C and C++. For example:

#define PI 3.14159
This defines a constant PI with the value 3.14159.

const keyword: The const keyword is used to define constants in C++. For example:

const double PI = 3.14159;
This defines a constant PI with the value 3.14159.

enum keyword: The enum keyword is used to define a set of named integer constants in C and C++. For example:

enum Days { MON, TUE, WED, THU, FRI, SAT, SUN };
This defines a set of constants MON, TUE, WED, THU, FRI, SAT, and SUN with the values 0, 1, 2, 3, 4, 5, and 6, respectively.

Overall, constants are an important concept in C and C++ programming, and they are used to define fixed values that cannot be changed during the execution of a program. They can be defined using the #define preprocessor directive, the const keyword, or the enum keyword.