C Constants
Understanding fixed values in C programming
🔒 What are Constants?
Constants are fixed values that cannot be modified once they are defined during program execution. They are used to store values that remain unchanged throughout the program, such as mathematical constants (like PI), array sizes, or configuration values. Using constants not only makes your code more readable and maintainable but also helps prevent accidental modifications to important values and improves program reliability.
#include <stdio.h>
#define PI 3.14159
const int MAX_SIZE = 100;
int main() {
printf("Value of PI: %.2f\n", PI);
printf("Max size: %d\n", MAX_SIZE);
return 0;
}
Output:
Value of PI: 3.14 Max size: 100
Types of Constants
Integer Constants
Whole number values
const int age = 25;
#define YEAR 2024
Float Constants
Decimal number values
const float price = 19.99;
#define GRAVITY 9.8
Character Constants
Single character values
const char grade = 'A';
#define NEWLINE '\n'
String Constants
Text string values
const char name[] = "John";
#define GREETING "Hello"
🔹 Using #define Preprocessor
The #define directive creates symbolic constants replaced during preprocessing before compilation begins. Define constants using #define MAX_SIZE 100 to use MAX_SIZE throughout your code instead of hardcoded values. These replacements happen at compile-time, affecting code size and performance. Preprocessor constants enable easy code maintenance and creation of readable, reusable code patterns without runtime overhead associated with regular variables.
#include <stdio.h>
#define MAX_STUDENTS 50
#define PASS_MARK 60
#define SCHOOL_NAME "ABC High School"
int main() {
printf("School: %s\n", SCHOOL_NAME);
printf("Maximum students: %d\n", MAX_STUDENTS);
printf("Pass mark: %d\n", PASS_MARK);
int total_students = 45;
if (total_students < MAX_STUDENTS) {
printf("Can admit %d more students\n", MAX_STUDENTS - total_students);
}
return 0;
}
Output:
School: ABC High School Maximum students: 50 Pass mark: 60 Can admit 5 more students
🔹 Using const Keyword
The const keyword creates typed constants checked by the compiler for type safety and scope management. Declare constants using const int MAX_SIZE = 100; to create constants that respect variable scope rules and type checking. Unlike #define, const variables exist in memory and support pointer operations. Using const provides better debugging, type safety, and integration with modern C programming practices and conventions.
#include <stdio.h>
int main() {
const int days_in_week = 7;
const float tax_rate = 0.08;
const char currency = '$';
float price = 100.0;
float total = price + (price * tax_rate);
printf("Price: %c%.2f\n", currency, price);
printf("Tax rate: %.2f%%\n", tax_rate * 100);
printf("Total: %c%.2f\n", currency, total);
printf("Days in week: %d\n", days_in_week);
return 0;
}
Output:
Price: $100.00 Tax rate: 8.00% Total: $108.00 Days in week: 7
🔹 Difference: #define vs const
Choose between #define and const based on your specific needs for constants and symbolic values. #define operates at preprocessor level with simple text replacement, no type checking, and global scope. const provides type safety, respects scope, enables pointer operations, and integrates better with debugging tools. Modern C programming typically prefers const for better practice, though #define serves specific scenarios requiring compile-time substitution.
#include <stdio.h>
// #define - preprocessor replacement
#define PI 3.14159
#define MAX_SIZE 100
int main() {
// const - typed constant variable
const int array_size = 50;
const float radius = 5.0;
// Using #define constants
float area = PI * radius * radius;
printf("Area of circle: %.2f\n", area);
// Using const variables
int numbers[array_size]; // Valid in C99 and later
printf("Array size: %d\n", array_size);
// #define is replaced by preprocessor
printf("MAX_SIZE: %d\n", MAX_SIZE);
return 0;
}
Output:
Area of circle: 78.54 Array size: 50 MAX_SIZE: 100