A constant is a value your program should never change, such as a tax rate or a maximum size. C gives you three ways to create one: const, #define and enum.
This tutorial covers the fundamentals of constants, their syntax, properties, differences from literals, practical applications, common pitfalls, and best practices.
Why Use Constants?
What is a Constant?
A constant is a named value that remains fixed throughout the execution of a program. Unlike variables, once a constant is assigned a value, that value cannot be changed (at least not through legitimate means). Constants can be of any data type: integer, floating-point, character, string, or even user-defined types (with some limitations).
Example:
const int maxAttempts = 3; const double pi = 3.14159; const char newline = '\n';
In the above, maxAttempts, pi, and newline are constants—attempting to modify them later will result in a compile-time error.
Why Use Constants?
- Prevents accidental modification: Protects values that should never change (e.g., physical constants, configuration limits).
- Improves readability: Names like
MAX_BUFFER_SIZEare more meaningful than magic numbers like1024. - Easier maintenance: Changing a constant’s value in one place updates it everywhere it’s used.
- Enables compiler optimizations: The compiler can assume the value never changes and optimize accordingly.
- Documents intent: Readers understand that a value is fixed by design.
Types of Constants in C
C distinguishes between:
- Literal constants: Fixed values that appear directly in source code (e.g.,
42,3.14,'A',"hello"). These are not named; they are the values themselves. - Symbolic constants (
#define): Preprocessor macros that are textually replaced by their value before compilation (e.g.,#define PI 3.14). - Const-qualified variables (
const): Actual variables with a type that cannot be modified after initialization (e.g.,const int limit = 100;). - Enumeration constants (
enum): Named integer constants defined usingenum.
This tutorial focuses primarily on const and #define.
Constants vs. Literals
Many newcomers confuse constants with literals. Here is the distinction:
| Aspect | Constant | Literal |
|---|---|---|
| Definition | A named variable that cannot be modified after initialization | A fixed value that appears directly in code |
| Created by | const keyword or #define | Written as a value (e.g., 10, 'x', "text") |
| Has address? | Yes (for const variables) | No (except string literals, which are arrays) |
| Lvalue/Rvalue | Lvalue (can appear on left of assignment, but assignment prohibited) | Rvalue (only appears on right of assignment) |
| Example | const int c = 20; | 20 |
In const int c = 20;, c is a constant variable, and 20 is an integer literal.
const vs #define vs enum
Using the const Keyword
The const keyword is a type qualifier that makes a variable read-only. Syntax:
const data_type variable_name = value;
The const qualifier can appear before or after the type (though the position relative to pointers matters).
Examples:
const int maxUsers = 100; int const minUsers = 10; // equivalent const float pi = 3.14159f; const char delimiter = ','; const char message[] = "Hello"; // constant array of characters
Properties of const Variables
- Must be initialized at declaration (for automatic storage duration). If not initialized, the variable contains a garbage value, and since it cannot be assigned later, it’s useless.cconst int a; // Warning/error: uninitialized const printf(“%d”, a); // Undefined behavior if not initializedNote: Some compilers allow this but issue a warning. The value is garbage (often zero, but not guaranteed).
- Immutable after initialization: Any attempt to modify a
constvariable results in a compile-time error.cconst int a = 10; a = 20; // Error: assignment of read-only variable ‘a’ - Type safety: The compiler knows the type of a
constvariable, enabling type checking and better optimization. - Scope control:
constvariables follow normal scoping rules. They can be local, global, or static. - Addressable: You can take the address of a
constvariable (though modifying through that address is undefined behavior if the original wasconst).
Using #define for Constants
The #define directive creates a macro that is textually replaced by its value during preprocessing. Syntax:
#define CONSTANT_NAME value
Example:
#include <stdio.h>
#define PI 3.14
#define MAX_SIZE 100
int main(void) {
printf("%.2f\n", PI);
int arr[MAX_SIZE]; // replaced by int arr[100];
return 0;
}
Key characteristics:
- No type information; it’s a simple text substitution.
- Handled by the preprocessor, not the compiler.
- No scope; the macro is visible from the point of definition to the end of the file (or until
#undef). - Cannot be debugged directly (debugger sees expanded value, not the macro name).
- Can be used for any token sequence, not just numbers (e.g.,
#define BEGIN {).
const vs. #define
| Aspect | const | #define |
|---|---|---|
| Type safety | Yes | No |
| Scope | Normal variable scope | File scope (from definition to end) |
| Debugging | Visible in debugger | Not visible (replaced before compile) |
| Memory | Allocates storage (unless optimized) | No storage (text replacement) |
| Address | Can take address | Cannot take address |
| Handled by | Compiler | Preprocessor |
Recommendation: Prefer const for typed constants in modern C. Use #define for macros that need textual substitution (e.g., array sizes in C89, or conditional compilation).
Enumeration Constants
enum defines a set of named integer constants.
enum Color { RED, GREEN, BLUE }; // RED=0, GREEN=1, BLUE=2
enum Status { OK = 200, NOT_FOUND = 404, ERROR = 500 };
Enum constants have type int and are visible in the enclosing scope. They are useful for related sets of values.
Constants in a Working C Program
This program demonstrates various types of constants in a realistic banking application: withdrawal limits, interest calculation, and login attempts. It shows proper use of const, #define, and enum.
Program: Banking Constant Demo
/**
* banking_constants.c
* Demonstrates use of constants in a banking context.
* Covers const variables, #define macros, and enum constants.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/* Macro constants (preprocessor) */
#define MAX_NAME_LEN 50
#define MAX_LINE_LEN 100
#define ANNUAL_INTEREST_RATE 0.035 /* 3.5% annual interest */
/* Enum constants for account types */
typedef enum {
ACCOUNT_SAVINGS = 1,
ACCOUNT_CHECKING,
ACCOUNT_FIXED_DEPOSIT
} AccountType;
/* Global const variables */
const double MIN_BALANCE_SAVINGS = 100.0;
const double MIN_BALANCE_CHECKING = 50.0;
const int MAX_LOGIN_ATTEMPTS = 3;
/* Function prototypes */
int readDouble(const char *prompt, double *outValue);
int readInteger(const char *prompt, int *outValue);
int main(void) {
/* Local const variables */
const double dailyWithdrawalLimit = 20000.0;
char accountHolder[MAX_NAME_LEN];
double balance = 0.0;
double withdrawalAmount;
int accountType;
int loginAttempts = 0;
/* Read account holder name */
printf("Enter account holder name: ");
fflush(stdout);
if (fgets(accountHolder, sizeof(accountHolder), stdin) == NULL) {
fprintf(stderr, "Error reading name.\n");
return EXIT_FAILURE;
}
accountHolder[strcspn(accountHolder, "\n")] = '\0';
/* Select account type */
printf("\nSelect account type:\n");
printf("1. Savings (Min Balance: $%.2f)\n", MIN_BALANCE_SAVINGS);
printf("2. Checking (Min Balance: $%.2f)\n", MIN_BALANCE_CHECKING);
printf("3. Fixed Deposit\n");
printf("Enter choice: ");
fflush(stdout);
if (readInteger("", &accountType) != 0) {
fprintf(stderr, "Failed to read account type.\n");
return EXIT_FAILURE;
}
/* Validate account type using enum constants */
if (accountType != ACCOUNT_SAVINGS &&
accountType != ACCOUNT_CHECKING &&
accountType != ACCOUNT_FIXED_DEPOSIT) {
fprintf(stderr, "Invalid account type.\n");
return EXIT_FAILURE;
}
/* Set minimum balance based on account type */
const double minimumBalance = (accountType == ACCOUNT_SAVINGS) ?
MIN_BALANCE_SAVINGS :
MIN_BALANCE_CHECKING;
/* Simulate login attempts */
printf("\nSimulating login attempts (max %d):\n", MAX_LOGIN_ATTEMPTS);
for (int i = 0; i < MAX_LOGIN_ATTEMPTS; i++) {
loginAttempts++;
printf("Login attempt %d of %d\n", loginAttempts, MAX_LOGIN_ATTEMPTS);
}
if (loginAttempts >= MAX_LOGIN_ATTEMPTS) {
printf("Maximum login attempts reached. Account locked.\n");
}
/* Demonstrate daily withdrawal limit */
printf("\nDaily withdrawal limit: $%.2f\n", dailyWithdrawalLimit);
/* Initial deposit */
double initialDeposit;
if (readDouble("Enter initial deposit: $", &initialDeposit) != 0) {
fprintf(stderr, "Invalid deposit.\n");
return EXIT_FAILURE;
}
balance += initialDeposit;
printf("Balance: $%.2f\n", balance);
/* Withdrawal with limit check */
if (readDouble("Enter withdrawal amount: $", &withdrawalAmount) != 0) {
fprintf(stderr, "Invalid withdrawal.\n");
return EXIT_FAILURE;
}
if (withdrawalAmount > dailyWithdrawalLimit) {
printf("Cannot withdraw $%.2f. Exceeds daily limit of $%.2f.\n",
withdrawalAmount, dailyWithdrawalLimit);
} else if (balance - withdrawalAmount < minimumBalance) {
printf("Cannot withdraw $%.2f. Minimum balance required.\n", withdrawalAmount);
} else {
balance -= withdrawalAmount;
printf("Withdrew $%.2f. New balance: $%.2f\n", withdrawalAmount, balance);
}
/* Calculate annual interest */
double interest = balance * ANNUAL_INTEREST_RATE;
printf("\nAnnual interest at %.1f%%: $%.2f\n",
ANNUAL_INTEREST_RATE * 100, interest);
return EXIT_SUCCESS;
}
/* Reads an integer from stdin using fgets and strtol. */
int readInteger(const char *prompt, int *outValue) {
char buffer[MAX_LINE_LEN];
char *endPtr;
long val;
while (1) {
if (prompt[0] != '\0') {
printf("%s", prompt);
fflush(stdout);
}
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
return -1;
}
if (strchr(buffer, '\n') == NULL && !feof(stdin)) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
printf("Input too long, please try again.\n");
continue;
}
errno = 0;
val = strtol(buffer, &endPtr, 10);
if (endPtr == buffer || (*endPtr != '\n' && *endPtr != '\0')) {
printf("Invalid integer. Please try again.\n");
continue;
}
if (errno == ERANGE || val < -2147483647 - 1 || val > 2147483647) {
printf("Number out of range. Please try again.\n");
continue;
}
*outValue = (int)val;
return 0;
}
}
/* Reads a double from stdin using fgets and strtod. */
int readDouble(const char *prompt, double *outValue) {
char buffer[MAX_LINE_LEN];
char *endPtr;
double val;
while (1) {
if (prompt[0] != '\0') {
printf("%s", prompt);
fflush(stdout);
}
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
return -1;
}
if (strchr(buffer, '\n') == NULL && !feof(stdin)) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
printf("Input too long, please try again.\n");
continue;
}
errno = 0;
val = strtod(buffer, &endPtr);
if (endPtr == buffer || (*endPtr != '\n' && *endPtr != '\0')) {
printf("Invalid number. Please try again.\n");
continue;
}
if (errno == ERANGE) {
printf("Number out of range. Please try again.\n");
continue;
}
*outValue = val;
return 0;
}
}
Explanation of Constant Usage
MAX_NAME_LEN,MAX_LINE_LEN,ANNUAL_INTEREST_RATEare#definemacros used for buffer sizes and a rate.AccountTypeis anenumdefining named integer constants for account types.MIN_BALANCE_SAVINGS,MIN_BALANCE_CHECKING,MAX_LOGIN_ATTEMPTSare globalconstvariables with type information.dailyWithdrawalLimitis a localconstvariable specific tomain.minimumBalanceis aconstvariable initialized based on account type—its value cannot change after initialization.
Expected Output
Sample run:
Enter account holder name: John Doe Select account type: 1. Savings (Min Balance: $100.00) 2. Checking (Min Balance: $50.00) 3. Fixed Deposit Enter choice: 1 Simulating login attempts (max 3): Login attempt 1 of 3 Login attempt 2 of 3 Login attempt 3 of 3 Maximum login attempts reached. Account locked. Daily withdrawal limit: $20000.00 Enter initial deposit: $5000 Balance: $5000.00 Enter withdrawal amount: $25000 Cannot withdraw $25000.00. Exceeds daily limit of $20000.00. Annual interest at 3.5%: $175.00
Constant Mistakes and Surprises
1. Not Initializing const Variables at Declaration
A const variable cannot be assigned after declaration, so it must be initialized at the point of declaration (for automatic variables). Forgetting to initialize leads to a garbage value.
const int a; // Warning: 'a' is uninitialized
printf("%d\n", a); // Undefined behavior
Solution: Always initialize const variables at declaration.
2. Attempting to Modify Through a Non-const Pointer
You can cast away const and modify through a pointer, but this is undefined behavior if the original object was declared const.
const int x = 10; int *p = (int*)&x; // Cast away const *p = 20; // Undefined behavior
Solution: Never cast away const unless you are absolutely certain the original object is not truly constant.
3. Confusing const Pointer vs. Pointer to const
The placement of const relative to * matters:
const int *p1; // pointer to const int (pointee cannot change) int const *p2; // same as p1 int *const p3; // const pointer to int (pointer cannot change) const int *const p4; // const pointer to const int
Misunderstanding these leads to compile errors or logical bugs.
Example:
int a = 10, b = 20; const int *p = &a; *p = 30; // Error: cannot modify through pointer to const p = &b; // OK: pointer itself can change int *const q = &a; *q = 30; // OK: pointee can change q = &b; // Error: pointer is const
4. const Global Variables and Linkage
In C, const global variables have external linkage by default (unlike C++). If a header file defines a const variable and is included in multiple source files, you get multiple definition errors.
// header.h const int MAX = 100; // Defined in every file that includes header // file1.c and file2.c both include header.h -> link error
Solution: Define static const int MAX = 100; in the header, or declare extern const int MAX; in the header and define in one .c file.
5. Using #define Without Parentheses
Macros are text substitutions. Missing parentheses can cause precedence issues.
#define SQUARE(x) x * x int y = SQUARE(2 + 3); // Becomes 2 + 3 * 2 + 3 = 11, not 25
Solution: Always parenthesize macro arguments and the entire expression:
#define SQUARE(x) ((x) * (x))
6. No Type Checking with #define
#define macros have no type. Passing wrong types can lead to subtle bugs.
#define MAX(a, b) ((a) > (b) ? (a) : (b)) int x = 5; double y = 3.14; double z = MAX(x, y); // Mixed types, may not behave as expected
Solution: Prefer inline functions or _Generic (C11) for type-safe operations.
7. Memory Considerations
constvariables occupy memory (stack or data segment) unless the compiler optimizes them away.#definemacros do not occupy memory; they are textually replaced.- String literals are stored in read-only memory segments (often
.rodata). Modifying a string literal is undefined behavior:cchar *s = “hello”; s[0] = ‘H’; // Undefined behavior (attempting to modify read-only memory)Usechar s[] = "hello";if you need a modifiable string.
8. const and Function Parameters
Using const for pointer parameters documents that the function will not modify the pointed-to data, enabling compiler optimizations and preventing accidental changes.
void printData(const int *data, size_t len) {
for (size_t i = 0; i < len; i++) {
printf("%d ", data[i]);
}
// data[0] = 10; // Error: cannot modify through const pointer
}
9. Enum Constants Are Integers
Enum constants are of type int. They cannot be floating-point or strings. For non-integer constants, use const or #define.
Choosing the Right Kind of Constant
Constants in C protect values from modification, improve readability, and enable compiler optimizations. Choose the right mechanism based on your needs: const for typed, scoped constants; #define for simple textual substitution and conditional compilation; enum for related integer constants.
Key Takeaways
constcreates immutable variables;#definecreates macros replaced before compilation.constvariables must be initialized at declaration (for automatic storage).constprovides type safety, scope control, and debuggability.#definehas no type and no scope; it’s handled by the preprocessor.- String literals are constants stored in read-only memory.
- Pointer to
constvs.constpointer are different; mind the*placement.
Best Practices
- Prefer
constover#definefor typed constants in modern C (C99 and later). - Initialize
constvariables at declaration to avoid garbage values. - Use
static constfor file-private constants to avoid linkage issues. - Use
enumfor sets of related integer constants. - Use
#definefor macros that require textual substitution (e.g., array sizes in C89, conditional compilation). - Parenthesize macro arguments and expressions to avoid precedence bugs.
- Never modify string literals; use character arrays if mutation is needed.
- Use
constfor pointer parameters to document non-modification. - Avoid casting away
constunless absolutely necessary and safe. - Compile with warnings (
-Wall -Wextra) to catch uninitializedconstand other issues.
By mastering constants, you’ll write safer, more maintainable C code that clearly expresses intent and protects critical values.
