Skip to content

Module 2 · Variables, Types, Input and Operators — topic 2 of 6

Constants

Values that must not change — π, a tax rate, an array size — should be constants. C offers literals, #define macros, const variables and enumeration constants, each with different behaviour.

Beginner 12 min read·Lesson 6 of 37 in C Programming

Learning objectives

  • Write integer, floating, character and string literals correctly
  • Choose between #define and const
  • Explain what const protects against

Definition

A constant is a value that cannot be modified during program execution. In C it may be a literal (42, 'A', 3.5), a preprocessor macro (#define), a const-qualified object, or an enumeration constant.

Introduction

A “magic number” is an unexplained value in code, such as if (age >= 18) or price * 0.18. When the rule changes you must find every copy. Naming such values as constants makes code self-explaining and easy to update.

C gives you several ways to make constants. They look similar but behave differently, and knowing the difference prevents subtle bugs.

Why it matters

Constants communicate intent: MAX_STUDENTS tells the reader far more than 60. They also let the compiler stop you from modifying something by accident — assigning to a const variable is a compile-time error.

Exam questions and interviews often ask the difference between #define and const; the answer reveals whether you understand the preprocessor.

Detailed explanation

Literals: decimal 42, octal 052 (leading zero!), hexadecimal 0x2A, long 42L, unsigned 42U; floating 3.14 (a double), 3.14f (a float), 1e-3; character 'A' (an int value 65 in ASCII); string "hello" (an array of char ending with '\0').

`#define NAME value` is a textual substitution done by the preprocessor. The macro has no type and no memory address; the compiler never sees the name, only the replaced text.

`const type name = value;` creates a real, typed, read-only variable. It obeys scope rules and appears in the debugger. You cannot assign to it after initialisation.

Enumeration constants (enum { RED, GREEN }) name related integer values; they get their own lesson later.

`#define``const`
Handled byPreprocessor (text)Compiler (typed object)
Type checkingNoneYes
ScopeFrom definition to end of fileNormal block/file scope
Visible in debuggerNoYes
Usable as array size at file scope (C11)YesNo — not an integer constant expression

Syntax

C
#define PI 3.14159 /* macro: no = and no semicolon */
const double GST = 0.18; /* typed read-only variable */
const int MAX = 100;
  • Never put = or ; in a #define — they would be pasted into your code.
  • A const must be initialised when declared, because it cannot be assigned later.
  • Constant names are conventionally written in capitals.

Code examples

Using both kinds

C
#include <stdio.h>
#define PI 3.14159
int main(void) {
const double radius = 2.5;
printf("Area: %.3f\n", PI * radius * radius);
return 0;
}
Output
Area: 19.635

Number literals in different bases

C
#include <stdio.h>
int main(void) {
printf("%d %d %d\n", 42, 052, 0x2A);
printf("%c has code %d\n", 'A', 'A');
return 0;
}
Output
42 42 42
A has code 65

052 is octal (5×8 + 2 = 42) — a leading zero changes the base.

More examples

The macro precedence trap

C
#include <stdio.h>
#define SUM_BAD a + b
#define SUM_GOOD (a + b)
int main(void) {
int a = 2, b = 3;
printf("Bad: %d\n", SUM_BAD * 10);
printf("Good: %d\n", SUM_GOOD * 10);
return 0;
}
Output
Bad:  32
Good: 50

Step-by-step explanation

  • SUM_BAD * 10 is replaced by the text a + b * 10 before compilation.
  • Multiplication binds tighter than addition, so it computes 2 + 30 = 32.
  • SUM_GOOD * 10 becomes (a + b) * 10, which is 5 * 10 = 50.
  • Lesson: always parenthesise macro bodies that are expressions.

Expected output

Running “The macro precedence trap” prints exactly:

TextOutput
Bad: 32
Good: 50

Practical applications

  • Header files define limits such as INT_MAX and EOF as macros.
  • Firmware defines register addresses and bit masks as constants.
  • Financial code stores tax rates as named constants so audits can verify them.

Best practices

  • Replace every magic number with a named constant.
  • Prefer const for typed values; use #define for array sizes at file scope and for conditional compilation.
  • Wrap every expression macro in parentheses.

Common mistakes

Semicolon in a #define

Avoid

#define LIMIT 10;
int marks[LIMIT];

Prefer

#define LIMIT 10
int marks[LIMIT];

The array becomes int marks[10;]; — a confusing syntax error far from the actual mistake.

Trying to change a const

Avoid

const int max = 5;
max = 6;

Prefer

int max = 5;
max = 6;

Assigning to a const object is a compile-time error. If the value must change, it is not a constant.

Important notes

  • A string literal must not be modified: char *s = "hi"; s[0] = 'H'; is undefined behaviour. Use char s[] = "hi"; for a modifiable copy.
  • In C (unlike C++), a const int is not an integer constant expression, so it cannot size a file-scope array or be a case label.

Key takeaways

  • Literals have types: 3.14 is double, 3.14f is float, 'A' is an int.
  • #define is text substitution; const is a typed read-only variable.
  • Parenthesise macro bodies and never end them with a semicolon.

Try it yourself

  1. 1
    Write a program that converts 5 km to miles using a named constant for the conversion factor 0.621371.
  2. 2
    Predict, then check, what printf("%d\n", 010 + 0x10); prints.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

5 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
  • BeginnerWhy does this fail to compile? `#define SIZE 10;` followed by `int a[SIZE];`
  • BeginnerWhat is the main difference between `#define PI 3.14` and `const double PI = 3.14;`?
Practise this topic