C Constants
Constants in C are fixed values that cannot be altered by the program during its execution. They play a crucial role in making your code more readable and maintainable by providing meaningful names to fixed values.
Key Topics
1. #define Constants
The #define
directive is used to define symbolic constants or macros in C. It is a preprocessor directive, so the substitution is made before the program is compiled.
Example: Defining a Constant Using #define
#include <stdio.h>
#define PI 3.14159
int main() {
printf("Value of PI: %f\n", PI);
return 0;
}
Output:
Value of PI: 3.141590
Code Explanation: The #define
directive creates a symbolic constant PI
, which replaces all instances of PI
with 3.14159
during preprocessing.
2. The const
Keyword
The const
keyword is used to declare variables whose values cannot be changed after initialization. It provides type safety and allows for better debugging.
Example: Using const
Variables
#include <stdio.h>
int main() {
const int MAX_USERS = 100;
printf("Maximum Users: %d\n", MAX_USERS);
// MAX_USERS = 200; // Error: assignment of read-only variable
return 0;
}
Code Explanation: The variable MAX_USERS
is declared as const
, making it read-only. Attempting to modify it will result in a compilation error.
3. Literals
Literals are fixed values assigned directly in the code. They can be of various types:
- Integer Literals: e.g.,
10
,0x1A
,075
- Floating-point Literals: e.g.,
3.14
,2.0e-5
- Character Literals: e.g.,
'A'
,'\n'
- String Literals: e.g.,
"Hello, World!"
Example: Using Literals
#include <stdio.h>
int main() {
int decimal = 10;
int octal = 012; // Octal literal (preceded by 0)
int hex = 0xA; // Hexadecimal literal (preceded by 0x)
float pi = 3.14159;
char newline = '\n';
char message[] = "Hello, World!";
printf("Decimal: %d\n", decimal);
printf("Octal: %d\n", octal);
printf("Hexadecimal: %d\n", hex);
printf("Pi: %f\n", pi);
printf("Message:%c%s\n", newline, message);
return 0;
}
Output:
Decimal: 10 Octal: 10 Hexadecimal: 10 Pi: 3.141590 Message: Hello, World!
Code Explanation: The program demonstrates the use of different types of literals. Note that octal and hexadecimal literals are automatically converted to decimal when printed using %d
.
4. Enumerations
Enumerations provide a way to define a set of named integer constants, improving code readability and maintainability.
Example: Defining an Enumeration
#include <stdio.h>
enum Weekday {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
int main() {
enum Weekday today = WEDNESDAY;
printf("Day number: %d\n", today);
return 0;
}
Output:
Day number: 3
Code Explanation: The enumeration Weekday
assigns integer values starting from 0
to each day. WEDNESDAY
corresponds to 3
.
Best Practices
- Use constants for values that should not change during program execution.
- Name constants in uppercase letters to distinguish them from variables.
- Prefer
const
over#define
for type safety. - Use enumerations to represent related constants for better code clarity.
Don'ts
- Don't attempt to modify the value of constants after they are defined.
- Don't use magic numbers; define them as constants with meaningful names.
- Don't neglect the scope of constants; define them in the appropriate context.
Key Takeaways
- Constants provide a way to use fixed values safely in your programs.
- The
#define
directive and theconst
keyword are two ways to define constants in C. - Literals are fixed values directly assigned in the code.
- Enumerations allow you to define a set of named integer constants.