Maco C
Maco C
1. Introduction to Macros
Macros in C are fragments of code which have been given a name. Whenever the name is
used, it is replaced by the contents of the macro. Macros are handled by the preprocessor
before actual compilation begins.s
They help in reducing code redundancy and improving efficiency by substituting code
segments at compile time.
💡 Macros are part of the preprocessing step — they are replaced before the compiler
actually starts translating the code.
🧠 They are useful in embedded systems where memory and performance optimization is
crucial.
📌 Macros do not consume memory like variables, which makes them ideal for defining
constants.
2. Defining a Macro
#define PI 3.14159
#define MAX(a, b) ((a) > (b) ? (a) : (b))
🔹 You can also define macros without values for conditional compilation:
#define FEATURE_ENABLED
#undef MAX
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
3. Types of Macros
linkedin@KeerthanaGaliveeti
8. Parameterized Macros with Default Behavior (emulated): Though not directly
supported, logic can be written using conditional macros for default-like behavior.
9. Empty Macros: Used as compile-time flags:
10.#define DEBUG_MODE
11. Nested Macros: A macro can expand into another macro.
12.#define VALUE 10
13.#define DOUBLE(x) (x + VALUE)
4. Advantages of Macros
Speed: Macros are faster since they don’t involve function call overhead.
Convenience: Useful for defining constants and inline code snippets.
Code Readability: Macros help in writing cleaner and reusable code.
Preprocessor Capability: Can be used for conditional compilation and platform-
specific code.
Low-level Programming: Helpful in direct memory or register manipulation.
Compact Code: Encourages shorthand expressions for repetitive logic.
5. Disadvantages of Macros
Example:
#define INCREMENT(x) x + 1
int a = INCREMENT(5) * 2; // Expands to 5 + 1 * 2 = 7, not 12!
// Correct approach:
#define INCREMENT_SAFE(x) ((x) + 1)
#define DOUBLE(x) x + x
int a = DOUBLE(5) * 2; // Think about operator precedence!
📉 Too many macros can lead to code bloat and reduced readability.
6. Macro Operators
linkedin@KeerthanaGaliveeti
Escaped Quotes and Backslashes: Needed for constructing macros that include
complex strings.
#define DEBUG
#ifdef DEBUG
printf("Debugging enabled\n");
#endif
This is very useful for writing portable code across different platforms or for
enabling/disabling features.
8. Undefining Macros
#undef PI
9. Best Practices
linkedin@KeerthanaGaliveeti
10. Real-world Applications of Macros
When you compile a C program, the preprocessor performs the macro expansion. Here's a
brief overview of what happens:
📄 You can inspect the result of macro expansion using gcc -E to preprocess only.
Macros and inline functions serve similar purposes but have key differences:
linkedin@KeerthanaGaliveeti
🧠 Inlining avoids macro pitfalls and improves maintainability.
#define REPEAT_2(x) x x
#define REPEAT_4(x) REPEAT_2(x) REPEAT_2(x)
REPEAT_4(printf("Hi\n";))
#define STEP 1
#include __FILE__
#undef STEP
Advanced users simulate looping behavior by including the same file with different macro
values — mostly seen in code generation.
e) File-Specific Logging
linkedin@KeerthanaGaliveeti
Encourage learners to analyze and fix macro issues:
Challenge 1:
💪 Fix:
Challenge 2:
#define MULTIPLY(x, y) x * y
int result = MULTIPLY(2 + 3, 4 + 1); // Output?
💪 Fix:
#ifdef __GNUC__
#define INLINE __inline__
#elif defined(_MSC_VER)
#define INLINE __inline
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
linkedin@KeerthanaGaliveeti
✅ Difference Between Macro and Enum
17. Conclusion
Macros are a powerful feature in C that allow code substitution before compilation. While
they can enhance performance and code readability, they must be used with caution to avoid
bugs and maintain code clarity.
Understanding macros not only improves your C programming skills but also lays the
foundation for understanding preprocessing in other languages.
🧠 Try replacing some repetitive patterns in your code with macros and see the difference!
💡 Experiment with #define, #ifdef, and variadic macros in small projects to build
confidence.
linkedin@KeerthanaGaliveeti
Macros in C - Coding Questions and Answers
#define SQUARE(x) x
* x int main() {
int a = 5;
int result = SQUARE(a
+ 1); printf("%d\n",
result); return 0;
}
// Output: 11
// Explanation: Expanded as a + 1 * a + 1 = 5 + 1 * 5 + 1 = 11
// Output: 36
#define PRINT_SUM(a, b) \
printf("Adding %d and %d\n", a, b);
\ printf("Sum = %d\n", (a + b))
int main() {
int x = 4, y =
6; PRINT_SUM(x,
y);
return 0;
}
linkedin@KeerthanaGaliveeti
// Output: Adding 4 and 6\nSum = 10
#define TO_STRING(x)
#x int main() {
printf("%s\n", TO_STRING(Hello
World)); return 0;
}
// Output: 10
#define DEBUG
int
main()
{ #ifd
ef
DEBUG
printf("Debug mode
ON\n"); #else
printf("Debug mode
OFF\n"); #endif
return 0;
}
linkedin@KeerthanaGaliveeti
Q7. Variadic Macro for Logging
int main() {
int x = 10;
LOG("Value of x: %d",
x); LOG("Just a
message"); return 0;
}
int main() {
unsigned int x = 0x00;
SET_BIT(x, 2);
TOGGLE_BIT(x, 2);
printf("x = 0x%X\n", x);
return 0;
}
// Output: x = 0x0
// Output: Max = 20
linkedin@KeerthanaGaliveeti
Q10. Macro Redefinition using #undef
#include
<stdio.h>
#define PI
3.14 int
main() {
printf("Old PI = %.2f\n", PI);
#undef PI
#define PI 3.14159
// Output: Fahrenheit: 32
== 0) int main() {
int n = 6;
if (IS_EVEN(n)) {
printf("Even\n");
} else {
linkedin@KeerthanaGaliveeti
printf("Odd\n");
}
return 0;
}
// Output: Even
// Output: x = 20, y = 10
#define INCREMENT(x) (x +
1) int main() {
int i = 5;
int result = INCREMENT(i++);
printf("Result: %d\n",
result); return 0;
}
linkedin@KeerthanaGaliveeti
}
// Output: Area = 20
int main() {
printf("Square = %d\n",
SQUARE(6)); return 0;
}
// Output: Square = 36
#define IS_VOWEL(ch)
// Output: Vowel
linkedin@KeerthanaGaliveeti
Q19. Macro to Find Absolute Value
// Output: Absolute: 10
// Output: Cube = 27
linkedin@KeerthanaGaliveeti