Constants in C Language

In the C programming language, constants are values that remain unchanged throughout the execution of a program. They are fixed values that cannot be modified during runtime. Constants are useful for defining values that should not be altered, such as mathematical constants, maximum or minimum values, and fixed parameters. In this tutorial, we will explore what constants are and demonstrate how to define them in C code.

Defining Constants in C

In C, constants are defined using the const keyword. The const keyword is used to declare variables as constants, indicating that their values cannot be changed once assigned. Here's the syntax for defining a constant in C:

In the above syntax, data_type represents the type of the constant, constant_name is the name given to the constant, and value is the initial value assigned to the constant.

Example:

Let's consider an example where we define constants for the mathematical values of pi and the speed of light:

In the above example, we define two constants: PI and SPEED_OF_LIGHT. PI is defined as a float constant with the value of 3.14159, representing the mathematical constant pi. SPEED_OF_LIGHT is defined as an int constant with the value of 299792458, representing the speed of light in meters per second. We then use printf() to display the values of these constants.

Output:

Note: It is a good practice to name constants in uppercase letters to differentiate them from variables.

By defining constants using the const keyword, we ensure that the values remain unchanged throughout the program, making the code more readable and maintainable.

List of Different Types of Constants in C

Here's a table showcasing different types of constants in C:

Constant TypeDescriptionExample
Integer ConstantsWhole numbers without a fractional part42, -10, 0
Floating-point ConstantsReal numbers with a fractional part3.14, -0.5, 2.0e-3
Character ConstantsIndividual characters enclosed in single quotes'A', '7', '$'
String ConstantsSequence of characters enclosed in double quotes"Hello, World!", "C"
Enumeration ConstantsUser-defined named constantsenum Day { SUNDAY, MONDAY, TUESDAY }
Macro ConstantsSymbolic constants defined using #define#define MAX_VALUE 100
Bitwise ConstantsConstants defined using bitwise operations#define FLAG_READ 0x01
Note: The examples provided in the table are just illustrative and may vary depending on your program's context and requirements.

Integer constants represent whole numbers without a fractional part. Floating-point constants represent real numbers with a fractional part. Character constants represent individual characters enclosed in single quotes. String constants represent a sequence of characters enclosed in double quotes. Enumeration constants are user-defined named constants, and macro constants are symbolic constants defined using the #define directive. Bitwise constants are constants defined using bitwise operations.

Example

In the above example, we declare variables and constants of different types. The age variable is an integer, the temperature variable is a floating-point number, the grade variable is a character, the name variable is a string, and the today variable is an enumeration constant. We use printf() to display the values of these variables and constants.

Output:

By utilizing different types of constants, you can represent various types of fixed values in your C programs, enhancing code readability and flexibility.