๐ C Constants and Literals โ Syntax, Types, and Best Practices
๐งฒ Introduction โ What Are Constants and Literals in C?
In C programming, constants and literals are used to represent fixed values that remain unchanged during the execution of a program. Constants improve code clarity and prevent accidental modification of important values. Literals, on the other hand, are the raw values directly written into your source code.
๐ฏ In this guide, youโll learn:
- The difference between constants and literals
- How to declare constants using
const
and#define
- The different types of literals in C
- Best practices for using constants and literals in programs
๐ What Is a Constant in C?
A constant is a variable-like entity whose value is fixed after its initialization. C supports two ways to define constants:
๐น const
Keyword
Declares a typed constant that is scoped and type-checked by the compiler.
const float PI = 3.14;
๐น #define
Preprocessor Macro
Defines a constant at the preprocessor level (no data type checking).
#define MAX_USERS 100
๐งพ Constant vs Variable
Property | Constant (const ) | Variable |
---|---|---|
Modifiability | โ Cannot be changed | โ Can be modified |
Type Safety | โ Type-checked | โ Type-checked |
Usage | Fixed value | Value may change |
Compiler Checked | At compile time | During execution |
๐ข What Is a Literal?
A literal is a raw, fixed value written directly into the source code. It is not named and cannot be modified.
๐ Types of Literals:
Type | Example | Description |
---|---|---|
Integer Literal | 42 , 0xA , 075 | Whole numbers (decimal, hex, octal) |
Floating-point Literal | 3.14 , -0.5 | Decimal numbers |
Character Literal | 'A' , '1' | Enclosed in single quotes |
String Literal | "Hello" | Enclosed in double quotes |
Boolean Literal (C99+) | true , false | Defined in <stdbool.h> as macros |
โ Example:
int age = 30; // 30 is an integer literal
char grade = 'A'; // 'A' is a character literal
float pi = 3.14159; // 3.14159 is a float literal
โ Constants with Expressions
Constants can also be initialized using constant expressions:
const int MAX = 5 * 3; // Result: 15
#define AREA(l, w) ((l) * (w)) // Macro for area calculation
๐ Best Practices
- Use
const
for scoped and type-safe constants - Use
#define
for global compile-time constants - Prefer meaningful names over magic numbers
- Use uppercase with underscores for constant names (
MAX_SIZE
,PI_VALUE
)
๐ Summary โ Recap & Next Steps
Constants and literals are essential tools for creating readable, reliable, and maintainable C programs. They protect fixed values from accidental modification and enhance the clarity of your logic.
๐ Key Takeaways:
- Use
const
for typed, read-only variables - Use
#define
for macro-level constants - Literals are direct values like
10
,3.14
,'A'
,"text"
- Avoid magic numbers by assigning them descriptive names
โ๏ธ Real-World Relevance:
Constants are critical in embedded systems, configuration values, limits, hardware register definitions, and moreโwhere stability and fixed data are non-negotiable.
โ Frequently Asked Questions (FAQ)
โ What is the difference between const
and #define
?
โ
const
declares a typed constant with scope, checked by the compiler.
โ
#define
is a macro that substitutes code during preprocessing without type safety.
โ Can a const
variable be modified?
โ
No. Attempting to change a const
variable will result in a compile-time error.
โ Are string literals modifiable?
โ No. String literals are stored in read-only memory. Attempting to modify them leads to undefined behavior.
โ Can I assign an expression to a constant?
โ Yes. Constants can be initialized using constant expressions:
const int LIMIT = 10 + 5; // โ
Valid
โ What is a magic number and why should it be avoided?
โ A “magic number” is a hardcoded value used directly in code without explanation. Replacing it with a named constant improves readability and maintainability.
Share Now :