#define Constants
Use preprocessor constants.
#define Constants is a free C Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What #define Does
#define is a preprocessor directive that creates a macro: a name the preprocessor replaces with text before compilation.
It is commonly used to give a readable name to a fixed value like a buffer size or a limit.
#define MAX_LEN 256Text Substitution
A macro is pure text replacement. Wherever MAX_LEN appears, the preprocessor writes 256 in its place.
There is no type and no memory; it happens before the compiler even sees the code.
#include <stdio.h>
#define MAX_LEN 256
int main(void) {
printf("%d\n", MAX_LEN);
return 0;
}No Semicolon
A common mistake is ending a #define with a semicolon.
The semicolon becomes part of the replacement text and can cause confusing errors. Leave it off.
#define PI 3.14159 /* correct */
#define BAD 10; /* wrong: semicolon is substituted too */Defining a Float Constant
Macros work for any literal, including floating-point values.
Here PI is substituted into the area formula at every use.
#include <stdio.h>
#define PI 3.14159
int main(void) {
double r = 2.0;
printf("%.2f\n", PI * r * r);
return 0;
}Macros for Array Sizes
Macros are often used to size arrays because the value must be known at compile time.
Using a name keeps the size in one place and easy to change.
#include <stdio.h>
#define SIZE 5
int main(void) {
int data[SIZE] = {1, 2, 3, 4, 5};
printf("%d\n", data[SIZE - 1]);
return 0;
}Always Parenthesize Expressions
If a macro expands to an expression, wrap it in parentheses.
Without parentheses, operator precedence can change the meaning when the macro is used inside a larger expression.
#define WIDTH (4 + 2) /* good */
#define BADW 4 + 2 /* risky: 10 * BADW becomes 10*4+2 */Why Parentheses Matter
This program shows the difference. With parentheses WIDTH multiplies as a whole; without them only part is multiplied.
The safe version prints 60, the unsafe expansion would compute differently.
#include <stdio.h>
#define WIDTH (4 + 2)
int main(void) {
printf("%d\n", 10 * WIDTH);
return 0;
}String Macros
Macros can also stand for string literals, which is handy for fixed messages or version names.
The text is substituted exactly, quotes included.
#include <stdio.h>
#define APP_NAME "CoddyKit"
int main(void) {
printf("Welcome to %s\n", APP_NAME);
return 0;
}#define vs const
A macro has no type and no address; a const variable has both and obeys scope.
Prefer const when you want type safety and debugging visibility. Use #define for compile-time constants like array sizes.
#define M_SIZE 10
const int c_size = 10;Undefining and Guards
You can remove a macro with #undef. Macros also power header guards that prevent double inclusion.
This shows the classic guard pattern used at the top of header files.
#ifndef CONFIG_H
#define CONFIG_H
/* header contents here */
#endifConventional Naming
Macro names are written in UPPER_CASE by long-standing convention.
This visually flags them as preprocessor constants and helps you avoid clashing with normal variable names.
#include <stdio.h>
#define BUFFER_SIZE 1024
int main(void) {
printf("%d\n", BUFFER_SIZE);
return 0;
}Quick Check
Test your understanding of macros.
Recap
#define creates macros that the preprocessor substitutes as text, with no type, no semicolon, and no memory.
Parenthesize expressions, and prefer const for type-safe values while reserving macros for compile-time needs like array sizes and header guards.
Frequently asked questions
Is the “#define Constants” lesson free?
Yes — the full text of “#define Constants” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “#define Constants”?
Use preprocessor constants. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “#define Constants” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining Enums
- Enum Values and Ranges
- const Variables
- #define Constants