Constants & Literals in C
A constant in C is a value that cannot be changed while the program is running.
Constants make programs safer, clearer, and prevent accidental modification of important data.
What Are Constants?
A constant is a fixed value used in a C program. Once assigned, its value cannot be altered during program execution.
Examples:
10, 3.14, 'A', "Hello"
Types of Constants in C
C supports several types of constants:
- Integer Constants
- Floating/Real Constants
- Character Constants
- String Constants
- Symbolic Constants
1. Integer Constants
These represent whole numbers (no decimal point).
10 -45 2500 0
Forms of Integer Constants
- Decimal (base 10) â 10, 45, -20
- Octal (base 8) â begins with 0 â 017, 025
- Hexadecimal (base 16) â begins with 0x â 0xFF, 0x1A
2. Floating (Real) Constants
These involve decimal values or exponential notation.
3.14 -2.50 6.02e23 7.0E-8
- Must contain at least one digit
- Must contain decimal point OR exponential part
3. Character Constants
A character constant contains a single character enclosed in single quotes.
'A' '5' '$' '\n' '\t'
Escape Characters
\n â newline \t â tab \\ â backslash \' â single quote \" â double quote
4. String Constants
Strings are sequences of characters enclosed in double quotes.
"Hello" "Welcome to C" "12345"
Every string ends with a hidden null character â
\0
5. Symbolic Constants
Symbolic constants are defined using #define.
They help avoid magic numbers and make code readable.
#define PI 3.14159 #define MAX 100
Once defined, PI and MAX cannot be changed.
Literal Values
"Literals" simply mean the raw values written directly in code.
10 // integer literal 3.5 // float literal 'A' // char literal "Hi" // string literal
Every literal is also a constant because its value is fixed.
Examples of Constants
const int age = 18; const float GST = 18.0; const char grade = 'A';
Using
const is a safer alternative to #define.
Difference Between const and #define
const: - Has data type - Checked by compiler - Stored in memory #define: - No data type - Preprocessor replacement - No memory allocated
Diagram: Types of Constants
CONSTANTS IN C
--------------------
| Integer |
| Floating |
| Character |
| String |
| Symbolic (#define) |
--------------------
Practical Example 1 â Using Constants & Literals
#include <stdio.h>
#define MAX 100
const float PI = 3.14;
int main() {
int roll = 25;
float radius = 7;
printf("Roll Number: %d\n", roll);
printf("Maximum Limit: %d\n", MAX);
printf("Circle Area: %.2f\n", PI * radius * radius);
return 0;
}
Practical Example 2 â Using Multiple Literal Types
#include <stdio.h>
int main() {
int day = 7; // integer literal
float temp = 32.6; // float literal
char grade = 'A'; // character literal
char name[] = "Sourav"; // string literal
printf("Day Number: %d\n", day);
printf("Temperature: %.1f°C\n", temp);
printf("Grade: %c\n", grade);
printf("Name: %s\n", name);
return 0;
}
Practical Example 3 â Using Symbolic Constants
#include <stdio.h>
#define GST 18
#define COMPANY "WebTechIO"
int main() {
float price = 2500;
float tax = (price * GST) / 100;
printf("Company: %s\n", COMPANY);
printf("Base Price: %.2f\n", price);
printf("GST (18%%): %.2f\n", tax);
printf("Total Price: %.2f\n", price + tax);
return 0;
}
Practical Example 4 â Using Escape Sequence Literals
#include <stdio.h>
int main() {
printf("Hello\\nWorld\n");
printf("Tabbed\\tText: \tSourav\n");
printf("Quote Example: \\\"C Programming\\\" \n");
printf("Backslash Example: \\\\ \n");
return 0;
}
Practical Example 5 â Constant Expressions
#include <stdio.h>
#define BASE 50
#define BONUS 20
int main() {
const int TOTAL = BASE + BONUS;
const float RATE = 2.5 * 3.2;
printf("Total Value: %d\n", TOTAL);
printf("Rate Value: %.2f\n", RATE);
return 0;
}
Practice Questions
- What are constants? List different types.
- Differentiate between integer and floating constants.
- What is a character constant? Give examples.
- What is the difference between const and #define?
- Write a program using #define to calculate area of a rectangle.
Practice Task
Write a C program that uses:
â 2 integer constants
â 1 float constant
â 1 symbolic constant
â 1 string literal
and prints all values clearly.
â 2 integer constants
â 1 float constant
â 1 symbolic constant
â 1 string literal
and prints all values clearly.