Constants in C

Constants in C are fixed values that do not change during program execution. They are one of the most important building blocks in the language because many programs rely on values that should stay stable, such as mathematical values, menu choices, status codes, limits, character markers, and configuration values. If a value is meant to remain unchanged, it should be treated as a constant rather than an ordinary variable.

This topic matters because constants improve clarity, reduce mistakes, and make programs easier to maintain. They also help you understand how C handles numbers, characters, strings, and named fixed values. In this article, we will understand what constants in C are, the main types of constants, the difference between literal and symbolic constants, how #define and const are used, and the mistakes beginners should avoid.

What are Constants in C?

A constant in C is a value that is not intended to change while the program runs. This fixed value can appear directly in the code, such as 10, 3.14, 'A', or "Hello", or it can be represented using a name such as MAX_SIZE or PI.

For example, in the expression area = 3.14 * r * r;, the value 3.14 acts as a constant. In the same way, if you write const int MAX = 100;, the named value MAX is also used as a constant in the program.

A variable is meant to store changeable data. A constant is meant to represent a value that should stay fixed.

Why Constants are Important in C

  • They make code easier to read and understand.
  • They reduce the use of unexplained magic numbers.
  • They prevent accidental modification of important values.
  • They make maintenance easier when a fixed value must be updated in one place.
  • They help express program logic more clearly.

For example, comparing a score against PASS_MARKS is much clearer than comparing it against a raw number like 40 everywhere in the program. Good use of constants improves both correctness and readability.

Types of Constants in C

Constants in C can be grouped into several common categories. These categories are useful because they show that constants are not only integers. Characters, floating values, and strings can also be constant values.

Type of ConstantExampleMeaning
Integer constant10, 0x2A, 075Whole-number constant
Floating constant3.14, 2.5e3Decimal or exponential numeric constant
Character constant'A', '\n'Single character constant
String constant"Hello"Sequence of characters ending with null terminator
Enumeration constantMON, REDNamed integer constant from an enum

Integer Constants

Integer constants are whole-number values without a fractional part. They can be written in decimal, octal, or hexadecimal form.

int decimal = 42;
int octal = 052;
int hex = 0x2A;

All three values above represent the same quantity, but they are written in different bases.

Floating Constants

Floating constants are numeric values with a fractional part or exponential notation. They are used when decimal precision is needed.

float pi = 3.14f;
double mass = 6.022e23;

Here 3.14f is a floating constant and 6.022e23 is written in scientific notation.

Character Constants

A character constant is written inside single quotes and represents one character value. In C, even character constants are stored as integer codes internally.

char grade = 'A';
char newline = '\n';

String Constants

A string constant is written inside double quotes. It represents a sequence of characters followed by a hidden null character \0.

char message[] = "Hello";

Even though it looks simple, a string constant is more than one character. It occupies multiple bytes in memory.

Enumeration Constants

Enumeration constants come from an enum definition. They are named integer constants that improve readability.

enum Day { MON, TUE, WED, THU, FRI };
enum Day today = WED;

Here MON, TUE, and WED are enumeration constants.

Literal Constants and Symbolic Constants

Beginners should understand one important distinction. Some constants are written directly in the code, while others are given meaningful names.

KindExampleMeaning
Literal constant100, 3.14, 'X'Value written directly in code
Symbolic constantMAX_SIZE, PINamed constant used instead of raw value

Literal constants are unavoidable, but using symbolic constants is often better for important program values because names carry meaning.

How to Define Constants in C

There are two common ways beginners encounter named constants in C: the #define preprocessor directive and the const keyword.

Using #define

#define creates a macro-style symbolic constant. The preprocessor replaces the name with the value before compilation.

#define PI 3.14159
#define MAX_STUDENTS 60

This is widely used for compile-time constants, fixed limits, and simple symbolic substitutions.

Using const

The const keyword is used with a typed object whose value should not be modified after initialization.

const int maxMarks = 100;
const float gravity = 9.8f;

This is still related to constants, but it also belongs to the broader topic of the const qualifier, which is why a deeper article on Const Keyword in C still makes sense later in the series.

Difference Between #define and const

Feature#defineconst
Type informationNo real typeHas a declared type
Handled byPreprocessorCompiler
Debug visibilityUsually weakerUsually better
Scope rulesText substitutionFollows normal scope rules
Typical useSymbolic compile-time replacementTyped read-only value

At the beginner level, both are used to represent fixed values, but they are not the same mechanism.

Constants in Real Programs

Constants appear in practical code much more often than beginners realize. Consider a program that checks pass marks, controls array sizes, uses menu choices, or prints a tax rate. All of these are good places for named constants.

#include <stdio.h>
#define PASS_MARKS 40

int main(void)
{
    int marks = 55;

    if (marks >= PASS_MARKS)
    {
        printf("Pass\n");
    }
    else
    {
        printf("Fail\n");
    }

    return 0;
}

Using PASS_MARKS makes the logic clearer than scattering the raw number 40 through the program.

Benefits of Using Named Constants

  • They remove magic numbers from the code.
  • They make intent clear to the next reader.
  • They reduce duplication of important values.
  • They simplify updates when a fixed value changes later.
  • They support more maintainable program design.

If a program uses the same fixed value in multiple places, a named constant is usually better than repeating the raw literal everywhere.

Common Mistakes with Constants in C

  • Confusing constants with variables that simply happen not to change.
  • Using too many unexplained literal numbers instead of named constants.
  • Assuming #define and const are identical.
  • Trying to modify a value declared with const.
  • Confusing character constants like 'A' with string constants like "A".
  • Mixing the broader idea of constants with the later, more specific topic of literals.
MistakeWhy It Causes TroubleBetter Approach
Using raw 100 everywhereMeaning may be unclearUse a named constant like MAX_LIMIT
Writing const int x = 5; x = 10;Attempts to modify read-only valueUse a normal variable if change is intended
Confusing 'A' with "A"One is a character, the other is a stringChoose the form that matches the data

Most beginner mistakes happen because the word constant sounds simple, but C supports several forms that behave differently.

Best Practices for Constants in C

  • Use named constants when a fixed value has program meaning.
  • Prefer clear names like MAX_USERS, PI, or PASS_MARKS.
  • Do not overuse raw literal numbers in logic-heavy code.
  • Understand when #define is simple text replacement and when const is a typed object.
  • Keep the distinction between constants, literals, and variables clear in your mind.

If you build this habit early, your C programs will become easier to read and less error-prone.

FAQs

What are constants in C?

Constants in C are fixed values that are not intended to change during program execution.

What are the types of constants in C?

The common types are integer constants, floating constants, character constants, string constants, and enumeration constants.

What is the difference between a variable and a constant in C?

A variable is meant to hold data that can change, while a constant represents a value that should remain fixed.

What is the difference between #define and const in C?

#define creates a preprocessor replacement, while const creates a typed value that the program should not modify.

Is a string constant a constant in C?

Yes. A string literal such as "Hello" is treated as a string constant in C.