Skip to content
StudyFame

Constants and Literals in C

Some values in a program should never change: the value of π, the number of days in a week, the GST rate. These fixed values are called constants.

In this lesson you will learn the types of constants in C and the two ways to create a named constant: the const keyword and #define.

What You Will Learn

  • What a constant (literal) is.
  • The 4 types of constants: integer, real, character and string.
  • Decimal, octal and hexadecimal integer constants.
  • How to make a constant with const and with #define.
  • The difference between const and #define.

Constants in C Explained

Exam definition: A constant is a fixed value that does not change during program execution. Constants written directly in the code, like 10 or 'A', are also called literals.

Types of constants

TypeMeaningExamples
Integer constantWhole numbers10, -25, 0
Real (floating) constantNumbers with a decimal point or exponent3.14, -0.5, 2.5e3 (= 2500)
Character constantOne character in single quotes'A', '7', '\n'
String constantCharacters in double quotes"Hello", "C", ""

Memory trick: "I R C S" = Integer, Real, Character, String. And: single quote = single character, double quotes = string.

Integer constants in different number systems

SystemHow to writeExampleValue in decimal
Decimal (base 10)Normal number2626
Octal (base 8)Starts with 003226
Hexadecimal (base 16)Starts with 0x0x1A26

Careful: 010 is not ten. The leading 0 makes it octal, so it equals 8.

Two ways to make a named constant

  • const keyword: const float PI = 3.14159f; creates a read-only variable with a type.
  • #define (symbolic constant): #define PI 3.14159 tells the preprocessor to replace the name PI with the value everywhere.
const#define
Handled byCompilerPreprocessor (text replacement)
Has a data type?YesNo
Uses memory?Yes, like a variableNo, the name is replaced by the value
Ends with ;?YesNo
ScopeFollows normal variable scopeFrom the #define line to the end of the file

Syntax of const and #define

const data_type NAME = value;   /* const keyword */
#define NAME value              /* symbolic constant, no = and no ; */

By custom, constant names are written in CAPITAL letters (PI, MAX_MARKS) so they are easy to spot.

Example 1: Area of a Circle Using const

#include <stdio.h>

int main(void)
{
    const float PI = 3.14159f;
    float radius = 7.0f;

    float area = PI * radius * radius;
    printf("Area = %.2f\n", area);

    return 0;
}

Output

Area = 153.94

Code Explanation

  1. const float PI = 3.14159f;: creates a float named PI that cannot be changed later.
  2. PI * radius * radius = 3.14159 × 7 × 7 = 153.938, printed as 153.94 with %.2f.
  3. If you write PI = 3; later, the compiler gives an error: assignment of read-only variable 'PI'.

How Constants Work

  • #define: before compilation, the preprocessor replaces every PI with 3.14159. The compiler never sees the name PI.
  • const: the compiler creates a real variable in memory but marks it read-only. Any attempt to change it is caught as an error.
  • Both protect you from changing a value by mistake, and make the code easier to update: change the value in one place, and it changes everywhere.

Example 2: Decimal, Octal and Hexadecimal Constants

#include <stdio.h>
#define DAYS_IN_WEEK 7

int main(void)
{
    printf("Decimal 10   = %d\n", 10);
    printf("Octal 012    = %d\n", 012);
    printf("Hex 0x1A     = %d\n", 0x1A);
    printf("Character 'A' = %d\n", 'A');
    printf("Days in a week = %d\n", DAYS_IN_WEEK);
    return 0;
}

Output

Decimal 10   = 10
Octal 012    = 10
Hex 0x1A     = 26
Character 'A' = 65
Days in a week = 7

Octal 12 = 1×8 + 2 = 10. Hex 1A = 1×16 + 10 = 26. The character 'A' is stored as its ASCII code, 65.

Important Points to Remember

  • A constant's value cannot change while the program runs.
  • 4 types: integer, real, character, string.
  • Octal starts with 0; hexadecimal starts with 0x.
  • const has a type; #define is text replacement.
  • No = and no ; in a #define line.

Common Mistakes Beginners Make

MistakeWhat happensFix
#define PI = 3.14;PI becomes "= 3.14;", breaking expressions#define PI 3.14
const int MAX; then MAX = 100;Error: a const must get its value when declaredconst int MAX = 100;
Writing 010 for tenIt is octal, equal to 8Write 10
'AB' as a characterA character constant holds one characterUse a string: "AB"

Constants in C Practice Questions

  1. What is a constant? Name its four types.
  2. What is the decimal value of 017 and 0x2F?
  3. Write two differences between const and #define.
  4. What is the difference between 'A' and "A"?
  5. Write a #define for the maximum marks, 100.
Show answers
  1. A fixed value that does not change during execution. Types: integer, real, character, string.
  2. 017 = 1×8 + 7 = 15. 0x2F = 2×16 + 15 = 47.
  3. const has a data type and is handled by the compiler; #define has no type and is replaced by the preprocessor.
  4. 'A' is a character constant (one character). "A" is a string constant (the character A plus a hidden '\0' at the end).
  5. #define MAX_MARKS 100

Coding Exercise

Use #define GST_RATE 18 and a const float price of 250. Calculate and print the GST amount and the final price.

Show solution
#include <stdio.h>
#define GST_RATE 18

int main(void)
{
    const float PRICE = 250.0f;
    float gst = PRICE * GST_RATE / 100;

    printf("GST: %.2f\n", gst);
    printf("Final price: %.2f\n", PRICE + gst);
    return 0;
}
GST: 45.00
Final price: 295.00

Quick Quiz: Constants in C (MCQ)

  1. Which is a character constant?

    A) "A"   B) 'A'   C) A   D) 65.0

  2. What is the decimal value of 0x10?

    A) 10   B) 8   C) 16   D) 2

  3. Which is correct?

    A) #define PI = 3.14;   B) #define PI 3.14   C) define PI 3.14   D) #define = PI 3.14

  4. #define constants are replaced by the:

    A) Compiler   B) Linker   C) Preprocessor   D) Loader

  5. What is 2.5e2?

    A) 25   B) 250   C) 2500   D) 0.025

Show answers
  1. B: 'A'
  2. C: 16.
  3. B: #define PI 3.14
  4. C: the preprocessor.
  5. B: 2.5 × 10² = 250.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

ConstantFixed value; cannot change while running
TypesInteger, Real, Character, String
Octal prefix0 (e.g. 012 = 10)
Hex prefix0x (e.g. 0x1A = 26)
Character constantSingle quotes: 'A'
String constantDouble quotes: "Hello"
Named constantsconst keyword and #define

Frequently asked questions

What is the difference between const and #define?
const is a typed read-only variable checked by the compiler; #define is text replacement by the preprocessor.
What is a literal?
A value written directly in the code, like 10, 3.14 or 'A'.
What is the difference between 'A' and "A"?
'A' is one character; "A" is a string of 2 bytes (A + '\0').
Can you change a const variable?
No. The compiler gives an error: assignment of read-only variable.

Remember

  • Types: I-R-C-S. Single quote = single character.
  • No =, no ; in #define.

Next Topic

Next lesson: Format Specifiers in C. You will learn all the % codes used to print and read different types of data.