Back to C Programming
2026-07-125 min read

C - Constants

Learn C - Constants step by step with clear examples and exercises.

Why This Matters

Learning about constants is essential for every C programmer, as they help to make your code more efficient and reliable. In this lesson, we'll explore what constants are, how to use them, common mistakes to avoid, practice questions, and frequently asked questions.

Why This Matters

In programming, constants are values that cannot be changed during the execution of a program. They provide several benefits:

  1. Improve code readability by making it clear which values should not change.
  2. Reduce errors due to accidental modification of variable values.
  3. Enhance code maintainability as changes to constant values require updating only in one place.
  4. Aid debugging by making it easier to identify the cause of issues when constants are involved.

Prerequisites

Before diving into C constants, you should have a basic understanding of:

  1. Variables and their types (int, float, char)
  2. Basic syntax for declaring variables
  3. Compilation process in C
  4. Understanding the difference between variables and constants

Core Concept

In C programming, there are two types of constants: integral and floating-point constants. Let's examine them one by one.

Integral Constants

Integral constants represent whole numbers without a fractional part. They can be of three types: decimal integer constant, octal integer constant, and hexadecimal integer constant.

Decimal Integer Constant

A decimal integer constant is represented using digits 0 through 9. For example:

int myNum = 1234;

Octal Integer Constant

An octal integer constant begins with the digit 0 and is followed by one or more octal digits (0-7). For example:

int myOctalNum = 0123;

Hexadecimal Integer Constant

A hexadecimal integer constant begins with the characters 0x or 0X, followed by one or more hexadecimal digits (0-9 and A-F in uppercase or lowercase). For example:

int myHexNum = 0xF0;

Floating-Point Constants

Floating-point constants represent real numbers with a fractional part. They can be of two types: decimal floating-point constant and scientific notation constant.

Decimal Floating-Point Constant

A decimal floating-point constant consists of an optional integer part, a decimal point, and an optional fractional part. For example:

float myFloat = 3.14159;

Scientific Notation Constant

A scientific notation constant consists of an integer part (coefficient), an e or E, and an exponent. The coefficient is followed by a decimal point and zero or more digits, while the exponent indicates the power to which 10 should be raised. For example:

float mySciNotation = 6.02e23; // Avogadro's number

Constants Qualifiers

In C programming, you can declare constants with const qualifiers to make them read-only and prevent accidental modification. There are two types of const qualifiers: const and constexpr.

const Qualifier

The const qualifier is used to declare a constant object that cannot be modified after its initialization. For example:

const int MY_CONSTANT = 42; // Cannot modify MY_CONSTANT
int *pConst = &MY_CONSTANT; // pConst points to a read-only memory location
*pConst = 99; // Compile error: assignment of read-only location

constexpr Qualifier (C++ only)

The constexpr qualifier is used to declare constants that can be evaluated at compile time. These constants can be used in places where expressions are allowed, such as within function templates and switch statements. For example:

// C++ only
constexpr int MY_CONSTANT = 42; // MY_CONSTANT can be evaluated at compile time
int array[MY_CONSTANT]; // Array size is determined at compile time

Worked Example

Let's create a simple program that demonstrates the use of constants and const qualifiers:

#include <stdio.h>

// Declare constant MY_PI with const qualifier
const float MY_PI = 3.14159;

int main() {
// Declare variables for radius and area
float radius, area;

// Assign value to radius and calculate the area using MY_PI
radius = 5.0;
area = MY_PI * radius * radius;

printf("The area of a circle with radius %f is: %f\n", radius, area);

return 0;
}

Output:

The area of a circle with radius 5.000000 is: 78.539816

Common Mistakes

  1. Incorrectly using const qualifiers: Remember that const and constexpr have different scopes and usage. Use them appropriately based on the context of your program.
  2. Misunderstanding the difference between variables and constants: Constants are immutable values, while variables can be modified during runtime.
  3. Not using const qualifiers when appropriate: Using const or constexpr can help to improve code readability and maintainability by making it clear which values should not change.
  4. Not understanding the difference between decimal floating-point constants and scientific notation constants: Be aware of how to write both types of floating-point constants correctly.
  5. Using const qualifiers on non-assignable variables: The const qualifier is unnecessary for variables that are inherently read-only, such as function parameters passed by value or constant pointers.

Practice Questions

  1. Declare a constant named MAX_INT with the maximum integer value that can be represented in C.
  2. Write a program that calculates the factorial of a number using a constant named FACTORIAL to store the result.
  3. Given the following code snippet, explain what is wrong and how to fix it:
const int myNum = 42;
int *pMyNum = &myNum;
*pMyNum = 99; // Compile error

FAQ

  1. Can I change the value of a constant in C?

No, constants cannot be changed once they are initialized in C.

  1. What is the difference between const and constexpr in C++?

const declares a read-only variable that can be modified only during initialization, while constexpr declares a constant that can be evaluated at compile time and used in expressions where variables are allowed.

  1. Can I use the const qualifier with arrays or pointers in C?

Yes, you can declare an array or pointer as const to make it read-only. However, the elements of the array cannot be modified if the entire array is declared as const.

  1. What happens when I try to modify a constant that was initialized with the const qualifier?

If you attempt to modify a constant that was initialized with the const qualifier, the compiler will generate an error and prevent the modification from taking place.

  1. Can I use the const qualifier with function parameters in C?

Yes, you can declare function parameters as const to indicate that they should not be modified within the function. However, if a function modifies its input arguments, it should pass them by reference (using pointers) instead of passing them by value (without the const qualifier).