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
constand with#define. - The difference between
constand#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
| Type | Meaning | Examples |
|---|---|---|
| Integer constant | Whole numbers | 10, -25, 0 |
| Real (floating) constant | Numbers with a decimal point or exponent | 3.14, -0.5, 2.5e3 (= 2500) |
| Character constant | One character in single quotes | 'A', '7', '\n' |
| String constant | Characters 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
| System | How to write | Example | Value in decimal |
|---|---|---|---|
| Decimal (base 10) | Normal number | 26 | 26 |
| Octal (base 8) | Starts with 0 | 032 | 26 |
| Hexadecimal (base 16) | Starts with 0x | 0x1A | 26 |
Careful: 010 is not ten. The leading 0 makes it octal, so it equals 8.
Two ways to make a named constant
constkeyword:const float PI = 3.14159f;creates a read-only variable with a type.#define(symbolic constant):#define PI 3.14159tells the preprocessor to replace the namePIwith the value everywhere.
const | #define | |
|---|---|---|
| Handled by | Compiler | Preprocessor (text replacement) |
| Has a data type? | Yes | No |
| Uses memory? | Yes, like a variable | No, the name is replaced by the value |
Ends with ;? | Yes | No |
| Scope | Follows normal variable scope | From 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
const float PI = 3.14159f;: creates a float namedPIthat cannot be changed later.PI * radius * radius= 3.14159 × 7 × 7 = 153.938, printed as 153.94 with%.2f.- 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 everyPIwith3.14159. The compiler never sees the namePI.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 with0x. consthas a type;#defineis text replacement.- No
=and no;in a#defineline.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
#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 declared | const int MAX = 100; |
Writing 010 for ten | It is octal, equal to 8 | Write 10 |
'AB' as a character | A character constant holds one character | Use a string: "AB" |
Constants in C Practice Questions
- What is a constant? Name its four types.
- What is the decimal value of
017and0x2F? - Write two differences between
constand#define. - What is the difference between
'A'and"A"? - Write a
#definefor the maximum marks, 100.
Show answers
- A fixed value that does not change during execution. Types: integer, real, character, string.
017= 1×8 + 7 = 15.0x2F= 2×16 + 15 = 47.consthas a data type and is handled by the compiler;#definehas no type and is replaced by the preprocessor.'A'is a character constant (one character)."A"is a string constant (the character A plus a hidden'\0'at the end).#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)
-
Which is a character constant?
A)
"A"B)'A'C)AD)65.0 -
What is the decimal value of
0x10?A) 10 B) 8 C) 16 D) 2
-
Which is correct?
A)
#define PI = 3.14;B)#define PI 3.14C)define PI 3.14D)#define = PI 3.14 -
#defineconstants are replaced by the:A) Compiler B) Linker C) Preprocessor D) Loader
-
What is
2.5e2?A) 25 B) 250 C) 2500 D) 0.025
Show answers
- B:
'A' - C: 16.
- B:
#define PI 3.14 - C: the preprocessor.
- 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
| Constant | Fixed value; cannot change while running |
|---|---|
| Types | Integer, Real, Character, String |
| Octal prefix | 0 (e.g. 012 = 10) |
| Hex prefix | 0x (e.g. 0x1A = 26) |
| Character constant | Single quotes: 'A' |
| String constant | Double quotes: "Hello" |
| Named constants | const keyword and #define |
Frequently asked questions
- What is the difference between const and #define?
constis a typed read-only variable checked by the compiler;#defineis text replacement by the preprocessor.- What is a literal?
- A value written directly in the code, like
10,3.14or'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.