← Back to C Course | Chapter 11: Preprocessor | Lesson 2 of 5

C #define & Macros

A macro made with #define is a shortcut name that gets swapped for other text before compiling, like a nickname. It can hold a constant or act like a tiny function.
Syntax
c
#define NAME value

#define MACRO(param) ((param) expression)

Object-like Macros

#define PI 3.14159 is the classic example -- every occurrence of PI in your code is textually replaced with 3.14159 before compilation, giving you a single place to update the value if it ever needs to change.

Example: Object-like Macros

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro PI (replaced by the preprocessor)
#define PI 3.14159
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.5f", PI);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function-like Macros

#define SQUARE(x) ((x) * (x)) looks like a function call at the point of use, but the preprocessor literally substitutes the text of the argument into the macro body wherever x appears, with no actual function call overhead at runtime.

Example: Function-like Macros

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro SQUARE (replaced by the preprocessor)
#define SQUARE(x) ((x) * (x))
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", SQUARE(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Macros with Multiple Arguments

#define MAX(a, b) ((a) > (b) ? (a) : (b)) demonstrates a two-argument macro -- each parameter name in the macro body gets replaced by the corresponding argument's literal text when the macro is used.

Example: Macros with Multiple Arguments

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro MAX (replaced by the preprocessor)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", MAX(3, 7));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Why Parentheses are Critical

Without them, SQUARE(x) defined as x * x would expand SQUARE(1+2) into 1+2 * 1+2, which evaluates incorrectly due to operator precedence -- wrapping every parameter and the whole expression in parentheses avoids this entire class of bugs.

Example: Why Parentheses are Critical

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro SQUARE (replaced by the preprocessor)
#define SQUARE(x) ((x) * (x))
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", SQUARE(1 + 2));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Undefining Macros with #undef

This is useful when a macro should only be active for part of your file, or when you want to redefine it to a different value for a different section of code without triggering a 'macro redefined' warning.

Example: Undefining Macros with #undef

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define the macro VALUE (replaced by the preprocessor)
#define VALUE 10
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d ", VALUE);
	#undef VALUE
	// Define the macro VALUE (replaced by the preprocessor)
	#define VALUE 20
	// Print formatted text to the screen
	printf("%d", VALUE);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting parentheses around macro arguments, so SQUARE(a + 1) expands to the wrong expression.
  2. Ending a #define with a semicolon, which becomes part of the replacement text.
  3. Passing an expression with side effects such as MAX(i++, j) to a macro, which may evaluate it twice.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.