Learn how to create constants that cannot be changed
What is this topic?
This guide explains C Constants - Create Unchanging Values in simple terms, what it does, and how to use it in real C programs.
Why We Need It
- It helps you write correct and reliable C code.
- It makes your programs easier to read and maintain.
- It is used in real projects and interviews.
- It reduces common beginner mistakes.
- It builds a strong foundation for advanced topics.
Use Cases
- Building practical C programs step by step.
- Solving real coding tasks with clean logic.
- Preparing for exams, interviews, and projects.
- Understanding and improving existing C code.
What is a Constant?
A constant is a value that cannot be changed after it’s set. It’s locked in place for the entire program.
Variables vs Constants
| Variables | Constants |
|---|---|
| Can be changed | Cannot be changed |
Use: int x = 5; |
Use: const int x = 5; |
| Flexible | Permanent |
Creating Constants with const
Syntax
const dataType constantName = value;
Example
#include <stdio.h>
int main() {
const float PI = 3.14159;
const int MAX_STUDENTS = 30;
const char GRADE = 'A';
printf("PI: %.5f\n", PI);
printf("Max Students: %d\n", MAX_STUDENTS);
printf("Grade: %c\n", GRADE);
return 0;
}
Output:
PI: 3.14159
Max Students: 30
Grade: A
Using Constants
Accessing Constants
const int WIDTH = 100;
const int HEIGHT = 50;
int area = WIDTH * HEIGHT;
printf("Area: %d\n", area); // 5000
What Happens If You Try to Change a Constant?
const int MAX = 100;
MAX = 200; // ✗ ERROR! Cannot change constant
The compiler will give you an error and prevent compilation.
Naming Convention for Constants
By convention, constant names are written in ALL UPPERCASE:
const float PI = 3.14159; // ✓ Good
const int MAX_ATTEMPTS = 5; // ✓ Good
const char CURRENCY = '$'; // ✓ Good
const float pi = 3.14159; // ✗ Poor - lowercase
const int maxAttempts = 5; // ✗ Poor - not all uppercase
Why Use Constants?
Reason 1: Prevent Accidental Changes
// Instead of this (easy to change by mistake)
int max_users = 100;
max_users = 50; // Oops! Changed by accident
// Do this (protected)
const int MAX_USERS = 100;
MAX_USERS = 50; // ✗ Compiler error - won't compile!
Reason 2: Self-Documenting Code
// Without constants - unclear
int result = salary * 0.15; // What is 0.15?
// With constants - clear
const float TAX_RATE = 0.15;
int result = salary * TAX_RATE; // Obviously 15% tax
Reason 3: Easy to Maintain
// If you need to change the value
const float PI = 3.14159265359; // Change once
// All calculations using PI automatically use new value
float area = PI * radius * radius;
float circumference = 2 * PI * radius;
#define - Another Way to Create Constants
You can also use preprocessor directives:
#include <stdio.h>
#define PI 3.14159
#define MAX_SIZE 100
int main() {
printf("PI: %f\n", PI);
printf("Max: %d\n", MAX_SIZE);
return 0;
}
Difference Between const and #define
| Feature | const | #define |
|---|---|---|
| Type checking | Yes | No |
| Memory used | Yes | No |
| Modern approach | Yes | Legacy |
| Recommended | ✓ Yes | For macros only |
Best Practice: Use const for most cases.
Practical Examples
Example 1: Circle Calculation
#include <stdio.h>
int main() {
const float PI = 3.14159;
const float RADIUS = 5.0;
float area = PI * RADIUS * RADIUS;
float circumference = 2 * PI * RADIUS;
printf("Radius: %.1f\n", RADIUS);
printf("Area: %.2f\n", area);
printf("Circumference: %.2f\n", circumference);
return 0;
}
Output:
Radius: 5.0
Area: 78.54
Circumference: 31.42
Example 2: Store Configuration
#include <stdio.h>
int main() {
const int MAX_ATTEMPTS = 3;
const float DISCOUNT_RATE = 0.10; // 10% discount
const char PASS_GRADE = 'C';
int attempts = MAX_ATTEMPTS;
float price = 100.00;
float discount = price * DISCOUNT_RATE;
float finalPrice = price - discount;
printf("Attempts left: %d\n", attempts);
printf("Original Price: $%.2f\n", price);
printf("Discount: $%.2f\n", discount);
printf("Final Price: $%.2f\n", finalPrice);
printf("Passing Grade: %c\n", PASS_GRADE);
return 0;
}
Output:
Attempts left: 3
Original Price: $100.00
Discount: $10.00
Final Price: $90.00
Passing Grade: C
Array Constants
You can create constant arrays:
const int DAYS_IN_WEEK = 7;
const int scores[] = {95, 87, 92, 88, 91};
// Can read but not modify
printf("Day 0: %d\n", scores[0]); // ✓ OK
scores[0] = 100; // ✗ Error!
Common Mistakes
Mistake 1: Changing a Constant
const int MAX = 100;
MAX = 200; // ✗ Compiler Error
Mistake 2: Not Initializing
const int MAX; // ✗ Error - must initialize
MAX = 100;
Fix:
const int MAX = 100; // ✓ Correct
Mistake 3: Lowercase Names
const int maxSize = 100; // ✗ Should be MAX_SIZE
Quick Reference
// Create constants
const int MAX = 100;
const float PI = 3.14159;
const char SYMBOL = '$';
// Using constants
int result = MAX * 2;
float area = PI * radius * radius;
printf("%c\n", SYMBOL);
// These cause errors:
// MAX = 200; ✗ Cannot change
// const int X; ✗ Must initialize
Practice Exercise
Create constants.c:
#include <stdio.h>
int main() {
const int BOOK_PRICE = 25;
const int QUANTITY = 4;
const float TAX_RATE = 0.08;
int subtotal = BOOK_PRICE * QUANTITY;
float tax = subtotal * TAX_RATE;
float total = subtotal + tax;
printf("=== Book Store Receipt ===\n");
printf("Book Price: $%d\n", BOOK_PRICE);
printf("Quantity: %d\n", QUANTITY);
printf("Subtotal: $%d\n", subtotal);
printf("Tax (8%%): $%.2f\n", tax);
printf("Total: $%.2f\n", total);
return 0;
}
Navigation
| Previous | Next |
|---|---|
| ← C Type Conversion | C Operators → |
Have Questions?
Comment below and we’ll help you! Questions about constants? Share them below!