Introduction
The C preprocessor works before the actual C compilation begins. It handles instructions such as #include, #define, #if, #ifdef, and #ifndef. Macros allow you to create reusable constants and small code-like expressions. In this chapter, you will practice preprocessor directives and macros through simple programs, gradually moving from basic #define usage to parameterized macros and conditional compilation. Preprocessor Directives and Macros in C practice questions with solutions to help you understand the concepts.
Q1. Create a Constant Using #define
Problem Statement
Write a C program that uses #define to create a constant for the value of Pi and calculate the area of a circle.
C Program
#include <stdio.h>
#define PI 3.14159
int main()
{
float radius;
float area;
radius = 5;
area = PI * radius * radius;
printf("Radius = %.2f\n", radius);
printf("Area = %.2f", area);
return 0;
}
Sample Output
Radius = 5.00
Area = 78.54
Explanation
This line:
#define PI 3.14159
creates a macro named PI.
Before compilation, the preprocessor replaces:
PI
with:
3.14159
So:
area = PI * radius * radius;
is effectively processed using the value 3.14159.
Concepts Covered
#define- Object-like macro
- Constants
- Preprocessor
Q2. Create Multiple Constants Using #define
Problem Statement
Use #define to create constants for the length and width of a rectangle and calculate its area and perimeter.
C Program
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
int main()
{
int area;
int perimeter;
area = LENGTH * WIDTH;
perimeter = 2 * (LENGTH + WIDTH);
printf("Area = %d\n", area);
printf("Perimeter = %d", perimeter);
return 0;
}
Sample Output
Area = 50
Perimeter = 30
Explanation
Two macros are created:
#define LENGTH 10
#define WIDTH 5
The preprocessor replaces these names with their values before compilation.
The formulas are:
Area = Length × Width
and:
Perimeter = 2 × (Length + Width)
Concepts Covered
- Multiple
#definedirectives - Constants
- Arithmetic expressions
- Preprocessor replacement
Q3. Create a Macro to Find the Square of a Number
Problem Statement
Create a macro named SQUARE that calculates the square of a number.
C Program
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
int main()
{
int number = 6;
printf("Square = %d", SQUARE(number));
return 0;
}
Sample Output
Square = 36
Explanation
This is a function-like macro:
#define SQUARE(x) ((x) * (x))
When you write:
SQUARE(number)
the preprocessor substitutes the argument into the macro.
For:
number = 6
the expression becomes effectively:
((6) * (6))
which gives:
36
The parentheses are important because they make the macro safer when expressions are passed as arguments.
Concepts Covered
- Function-like macros
- Macro parameters
#define- Parentheses in macros
Q4. Create a Macro to Find the Maximum of Two Numbers
Problem Statement
Create a macro named MAX that returns the larger of two numbers.
C Program
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main()
{
int x = 25;
int y = 40;
printf("Largest = %d", MAX(x, y));
return 0;
}
Sample Output
Largest = 40
Explanation
The macro is:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
It uses the conditional operator:
condition ? value_if_true : value_if_false
For:
x = 25
y = 40
the condition:
25 > 40
is false, so 40 is returned.
Concepts Covered
- Function-like macros
- Macro parameters
- Conditional operator
- Comparison
Q5. Use #ifdef for Conditional Compilation
Problem Statement
Write a program that displays an additional message when a macro named DEBUG is defined.
C Program
#include <stdio.h>
#define DEBUG
int main()
{
printf("Program is running.\n");
#ifdef DEBUG
printf("Debug mode is enabled.");
#endif
return 0;
}
Sample Output
Program is running.
Debug mode is enabled.
Explanation
The line:
#define DEBUG
defines the macro DEBUG.
Then:
#ifdef DEBUG
checks whether DEBUG has been defined.
If it has been defined, the code between:
#ifdef DEBUG
and:
#endif
is included during preprocessing.
If DEBUG is removed:
/* #define DEBUG */
the debug message will not be included.
Concepts Covered
#ifdef#endif- Conditional compilation
- Debug macros
Q6. Use #ifndef to Create a Default Value
Problem Statement
Use #ifndef to define a default value for MAX_SIZE only when it has not already been defined.
C Program
#include <stdio.h>
#ifndef MAX_SIZE
#define MAX_SIZE 100
#endif
int main()
{
printf("Maximum size = %d", MAX_SIZE);
return 0;
}
Sample Output
Maximum size = 100
Explanation
This code:
#ifndef MAX_SIZE
#define MAX_SIZE 100
#endif
means:
If
MAX_SIZEhas not already been defined, define it as100.
ifndef means:
if not defined
This technique is commonly used in header files to prevent repeated definitions.
Concepts Covered
#ifndef#define#endif- Conditional preprocessing
Q7. Create a Macro for Celsius to Fahrenheit Conversion
Problem Statement
Create a macro that converts a Celsius temperature into Fahrenheit.
Use the formula:
F = (C × 9 / 5) + 32
C Program
#include <stdio.h>
#define CELSIUS_TO_FAHRENHEIT(c) (((c) * 9.0 / 5.0) + 32)
int main()
{
float celsius = 25;
float fahrenheit;
fahrenheit = CELSIUS_TO_FAHRENHEIT(celsius);
printf("Celsius = %.2f\n", celsius);
printf("Fahrenheit = %.2f", fahrenheit);
return 0;
}
Sample Output
Celsius = 25.00
Fahrenheit = 77.00
Explanation
The macro is:
#define CELSIUS_TO_FAHRENHEIT(c) (((c) * 9.0 / 5.0) + 32)
When:
celsius = 25
the calculation becomes:
(25 × 9 / 5) + 32
which gives:
77
The decimal constants 9.0 and 5.0 ensure floating-point arithmetic.
Concepts Covered
- Function-like macros
- Mathematical expressions
- Floating-point calculation
- Macro parameters
Q8. Create a Macro to Check Whether a Number is Even
Problem Statement
Create a macro named IS_EVEN that determines whether a number is even.
C Program
#include <stdio.h>
#define IS_EVEN(x) ((x) % 2 == 0)
int main()
{
int number = 24;
if (IS_EVEN(number))
{
printf("%d is Even", number);
}
else
{
printf("%d is Odd", number);
}
return 0;
}
Sample Output
24 is Even
Explanation
The macro:
#define IS_EVEN(x) ((x) % 2 == 0)
returns a condition that is either true or false.
For:
24 % 2
the remainder is 0, so the condition is true.
Therefore:
if (IS_EVEN(number))
executes the first block.
Concepts Covered
- Function-like macros
- Modulus operator
- Boolean conditions
if-else
Q9. Use #if to Select Code
Problem Statement
Use a preprocessor constant to determine which message should be compiled into the program.
C Program
#include <stdio.h>
#define VERSION 2
int main()
{
#if VERSION == 1
printf("Version 1 selected.");
#elif VERSION == 2
printf("Version 2 selected.");
#else
printf("Unknown version.");
#endif
return 0;
}
Sample Output
Version 2 selected.
Explanation
The program defines:
#define VERSION 2
Then the preprocessor checks:
#if VERSION == 1
Since VERSION is 2, this condition is false.
Next:
#elif VERSION == 2
is true.
Therefore, the following code is included:
printf("Version 2 selected.");
The preprocessor decides which section is compiled before the compiler processes the resulting C code.
Concepts Covered
#if#elif#else#endif- Conditional compilation
- Preprocessor expressions
Q10. Create a Small Utility Library Using Macros
Problem Statement
Create several macros for common mathematical operations and use them in a C program.
C Program
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int main()
{
int a = 4;
int b = 7;
printf("Square of %d = %d\n", a, SQUARE(a));
printf("Cube of %d = %d\n", a, CUBE(a));
printf("Maximum = %d\n", MAX(a, b));
printf("Minimum = %d\n", MIN(a, b));
return 0;
}
Sample Output
Square of 4 = 16
Cube of 4 = 64
Maximum = 7
Minimum = 4
Explanation
This program creates four reusable macros:
#define SQUARE(x) ((x) * (x))
#define CUBE(x) ((x) * (x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
This lets you reuse common expressions without writing them repeatedly.
For example:
SQUARE(a)
calculates:
4 × 4 = 16
and:
MAX(a, b)
returns the larger value.
Concepts Covered
- Multiple macros
- Function-like macros
- Macro parameters
- Conditional operator
- Reusable expressions
Key Takeaways
- The C preprocessor works before compilation.
- Preprocessor directives begin with
#. #definecreates macros.- Object-like macros represent values or pieces of text.
- Function-like macros accept parameters.
- Parentheses make expression macros safer.
#ifdefchecks whether a macro is defined.#ifndefchecks whether a macro is not defined.#if,#elif,#else, and#endifprovide conditional compilation.#undefremoves a macro definition.- Macros are handled before the compiler processes the C program.
- Macros and functions are different and should not be treated as interchangeable.
FAQs
1. What is a preprocessor directive in C?
A preprocessor directive is an instruction beginning with # that is processed before the C compiler compiles the program.
2. What is #define in C?
#define is used to create macros.
Example:
#define PI 3.14159
3. What is a macro in C?
A macro is a named piece of text defined using #define. It can represent a constant or accept parameters.
4. What is the difference between #ifdef and #ifndef?
#ifdef checks whether a macro has been defined, while #ifndef checks whether it has not been defined.
5. What is a function-like macro?
A function-like macro accepts parameters.
Example:
#define SQUARE(x) ((x) * (x))
It looks similar to a function call but is expanded by the preprocessor.
6. Why should parentheses be used in macros?
Parentheses help prevent operator-precedence problems when a macro parameter or the complete macro expression contains operators.
7. Are macros the same as functions in C?
No. A macro is expanded by the preprocessor, while a function is compiled as a function and called at runtime. They also differ in type checking and argument evaluation behavior.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
