C Constants
In C programming, constants are fixed values that cannot be modified during program execution. They provide a way to define values that remain unchanged throughout your program. Using constants improves code readability, makes maintenance easier, and helps prevent accidental modifications of values that should remain fixed.
What are Constants?
A constant is an identifier whose value cannot be changed after its definition. Constants are used to represent fixed values like mathematical constants (π, e), physical constants, configuration values, or any value that should remain unchanged throughout the program.
Types of Constants in C
C supports several ways to define constants:
1. Literal Constants
Literals are the most basic form of constants. They are fixed values written directly in the source code.
Integer Literals
Integer literals are whole numbers without a decimal point.
42 // Decimal (base 10)
042 // Octal (base 8, starts with 0)
0x2A // Hexadecimal (base 16, starts with 0x)
Integer literals can also have suffixes to specify their type:
123L // long int
123U // unsigned int
123UL // unsigned long int
123LL // long long int (C99)
123ULL // unsigned long long int (C99)
Floating-Point Literals
Floating-point literals contain a decimal point and/or an exponent.
3.14159 // double by default
3.14159f // float (with f suffix)
3.14159L // long double (with L suffix)
1.23e2 // scientific notation (123.0)
1.23e-2 // scientific notation (0.0123)
Character Literals
Character literals represent a single character enclosed in single quotes.
'A' // the character A
'1' // the character 1 (not the integer 1)
'\n' // newline character
'\t' // tab character
'\0' // null character
'\\'' // backslash character
'\'' // single quote character
String Literals
String literals are sequences of characters enclosed in double quotes.
"Hello, World!" // a string
"This is a\nmultiline text" // with a newline character
"" // empty string
"C:\\Program Files\\" // with escaped backslashes
2. Defined Constants
Using #define
Directive
The preprocessor directive #define
is used to create symbolic constants:
#define PI 3.14159
#define MAX_SIZE 100
#define PROGRAM_NAME "Calculator"
// Usage
float area = PI * radius * radius;
int array[MAX_SIZE];
printf("Welcome to %s\n", PROGRAM_NAME);
Using const
Keyword
The const
keyword creates variables whose values cannot be modified:
const float pi = 3.14159;
const int max_size = 100;
const char* program_name = "Calculator";
// Usage
float area = pi * radius * radius;
int array[max_size];
printf("Welcome to %s\n", program_name);
When to Use Each Method
-
#define:
- For simple constant values
- When you need to use the constant in preprocessor conditions
- No type checking (just text substitution)
-
const keyword:
- When type safety is important
- For complex data types like arrays and structures
- When you need to limit the scope of a constant
- When you need to pass constants to functions
Example Program
Here's a complete example demonstrating the use of various constants:
#include <stdio.h>
// Using #define for constants
#define PI 3.14159
#define MAX_STUDENTS 50
#define SCHOOL_NAME "Programming Academy"
int main() {
// Using const keyword
const float g = 9.8; // Gravity constant
const int days_in_week = 7;
// Using literals
printf("Integer literals: %d, %d, %d\n", 10, 0xFF, 077);
printf("Floating point literals: %f, %e\n", 3.14, 1.23e-4);
printf("Character literals: %c, %c\n", 'A', '\n');
printf("String literal: %s\n", "Hello, World!");
// Using defined constants
printf("PI: %f\n", PI);
printf("Maximum students: %d\n", MAX_STUDENTS);
printf("School name: %s\n", SCHOOL_NAME);
// Using const variables
printf("Gravity: %f m/s²\n", g);
printf("Days in a week: %d\n", days_in_week);
return 0;
}
Best Practices
-
Use constants instead of "magic numbers" - Make your code more readable and maintainable.
-
Choose meaningful names - Names like
MAX_BUFFER_SIZE
are more descriptive thanMBSZ
. -
Use UPPERCASE for #define constants - This is a convention that helps distinguish constants from variables.
-
Prefer
const
over#define
for complex data types - Theconst
keyword provides type checking. -
Document the meaning of constants - Especially when the constants represent specific values with domain significance.
Exercises
- Create a program that calculates the area and circumference of a circle using a constant for π.
- Define constants for days of the week and use them in a switch statement.
- Create a program that uses various integer literals (decimal, octal, and hexadecimal) and displays their values.
- Compare the behavior of
#define
andconst
constants in different scopes of a program.
With a solid understanding of constants and literals, you'll be able to write more maintainable and readable C code.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)