#include <stdio.h>
#define SQUARE(x) ((x) * (x))
int main() {
int num = 5;
printf("Square of %d is: %d\n", num, SQUARE(num));
return 0;
}
What will be the output of this code?
Correct Answer: Square of 5 is: 25
Explanation: The `SQUARE` macro calculates the square of a number `x`. In this case, `SQUARE(num)` expands to `((num) * (num))`, resulting in 25.
#include <stdio.h>
#define CUBE(x) ((x) * (x) * (x))
int main() {
int num = 3;
printf("Cube of %d is: %d\n", num, CUBE(num + 1));
return 0;
}
What will be the output of this code?
Correct Answer: Cube of 4 is: 64
Explanation: The `CUBE` macro calculates the cube of a number `x`. In this case, `CUBE(num + 1)` expands to `((num + 1) * (num + 1) * (num + 1))`, resulting in 64.
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main() {
int x = 10, y = 20;
printf("Maximum of %d and %d is: %d\n", x, y, MAX(x, y));
return 0;
}
What will be the output of this code?
Correct Answer: Maximum of 10 and 20 is: 20
Explanation: The `MAX` macro returns the maximum of two numbers `a` and `b`. In this case, `MAX(x, y)` expands to `((x) > (y) ? (x) : (y))`, resulting in 20.
#include <stdio.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int main() {
int x = 15, y = 8;
printf("Minimum of %d and %d is: %d\n", x, y, MIN(x, y));
return 0;
}
What will be the output of this code?
Correct Answer: Minimum of 15 and 8 is: 8
Explanation: The `MIN` macro returns the minimum of two numbers `a` and `b`. In this case, `MIN(x, y)` expands to `((x) < (y) ? (x) : (y))`, resulting in 8.
#include <stdio.h>
#define ABS(x) ((x) < 0 ? -(x) : (x))
int main() {
int num = -5;
printf("Absolute value of %d is: %d\n", num, ABS(num));
return 0;
}
What will be the output of this code?
Correct Answer: Absolute value of -5 is: 5
Explanation: The `ABS` macro calculates the absolute value of a number `x`. In this case, `ABS(num)` expands to `((num) < 0 ? -(num) : (num))`, resulting in 5.
#include <stdio.h>
#define MAX3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
int main() {
int x = 10, y = 5, z = 15;
printf("Maximum of %d, %d, and %d is: %d\n", x, y, z, MAX3(x, y, z));
return 0;
}
What will be the output of this code?
Correct Answer: Maximum of 10, 5, and 15 is: 15
Explanation: The `MAX3` macro returns the maximum of three numbers `a`, `b`, and `c`. In this case, `MAX3(x, y, z)` expands to a nested conditional expression that computes the maximum, resulting in 15.
#include <stdio.h>
#define POWER(base, exp) ({ \
int result = 1; \
for (int i = 0; i < exp; ++i) \
result *= base; \
result; \
})
int main() {
int num = 2, exponent = 4;
printf("%d raised to the power of %d is: %d\n", num, exponent, POWER(num, exponent));
return 0;
}
What will be the output of this code?
Correct Answer: 2 raised to the power of 4 is: 16
Explanation: The `POWER` macro calculates the power of a `base` raised to an `exp` exponent using a for loop. In this case, `POWER(num, exponent)` expands to a block ({ ... }) that computes 2 raised to the power of 4, resulting in 16.
#include <stdio.h>
#define MULTIPLY(x, y) ((x) * (y))
int main() {
int a = 5, b = 4;
printf("Product of %d and %d is: %d\n", a, b, MULTIPLY(a + 1, b + 2));
return 0;
}
What will be the output of this code?
Correct Answer: Product of 5 and 4 is: 20
Explanation: The `MULTIPLY` macro calculates the product of two numbers `x` and `y`. In this case, `MULTIPLY(a + 1, b + 2)` expands to `((a + 1) * (b + 2))`, resulting in 20.
#include <stdio.h>
#define SUM(a, b) ((a) + (b))
int main() {
int x = 8, y = 5;
printf("Sum of %d and %d is: %d\n", x, y, SUM(x++, y--));
return 0;
}
What will be the output of this code?
Correct Answer: Sum of 8 and 5 is: 12
Explanation: The `SUM` macro calculates the sum of two numbers `a` and `b`. In this case, `SUM(x++, y--)` expands to `((x++) + (y--))`, resulting in 12.
#include <stdio.h>
#define DIFFERENCE(a, b) ((a) - (b))
int main() {
int x = 15, y = 7;
printf("Difference between %d and %d is: %d\n", x, y, DIFFERENCE(x--, y++));
return 0;
}
What will be the output of this code?
Correct Answer: Difference between 15 and 7 is: 8
Explanation: The `DIFFERENCE` macro calculates the difference between two numbers `a` and `b`. In this case, `DIFFERENCE(x--, y++)` expands to `((x--) - (y++))`, resulting in 8.
#include <stdio.h>
#define MAX4(a, b, c, d) (MAX(MAX(a, b), MAX(c, d)))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
int main() {
int w = 5, x = 10, y = 7, z = 12;
printf("Maximum of %d, %d, %d, and %d is: %d\n", w, x, y, z, MAX4(w, x, y, z));
return 0;
}
What will be the output of this code?
Correct Answer: Maximum of 5, 10, 7, and 12 is: 12
Explanation: The `MAX4` macro calculates the maximum of four numbers `a`, `b`, `c`, and `d` using nested `MAX` macros. In this case, `MAX4(w, x, y, z)` expands to `(MAX(MAX(w, x), MAX(y, z)))`, resulting in 12.
#include <stdio.h>
#define AREA(width, height) ((width) * (height))
int main() {
int w = 6, h = 4;
printf("Area of rectangle with width %d and height %d is: %d\n", w, h, AREA(w + 2, h - 1));
return 0;
}
What will be the output of this code?
Correct Answer: Area of rectangle with width 6 and height 4 is: 26
Explanation: The `AREA` macro calculates the area of a rectangle given its width and height. In this case, `AREA(w + 2, h - 1)` expands to `((w + 2) * (h - 1))`, resulting in 26.
#include <stdio.h>
#define AVERAGE(x, y) (((x) + (y)) / 2)
int main() {
int num1 = 9, num2 = 5;
printf("Average of %d and %d is: %d\n", num1, num2, AVERAGE(num1 * 2, num2 * 3));
return 0;
}
What will be the output of this code?
Correct Answer: Average of 9 and 5 is: 9
Explanation: The `AVERAGE` macro calculates the average of two numbers `x` and `y`. In this case, `AVERAGE(num1 * 2, num2 * 3)` expands to `(((num1 * 2) + (num2 * 3)) / 2)`, resulting in 9.
#include <stdio.h>
#define MAX5(a, b, c, d, e) (MAX(MAX(MAX(MAX(a, b), c), d), e))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
int main() {
int a = 5, b = 8, c = 12, d = 3, e = 9;
printf("Maximum of %d, %d, %d, %d, and %d is: %d\n", a, b, c, d, e, MAX5(a, b, c, d, e));
return 0;
}
What will be the output of this code?
Correct Answer: Maximum of 5, 8, 12, 3, and 9 is: 12
Explanation: The `MAX5` macro calculates the maximum of five numbers `a`, `b`, `c`, `d`, and `e` using nested `MAX` macros. In this case, `MAX5(a, b, c, d, e)` expands to `(MAX(MAX(MAX(MAX(a, b), c), d), e))`, resulting in 12.
#include <stdio.h>
#define CIRCLE_AREA(radius) (3.14 * (radius) * (radius))
int main() {
int r = 4;
printf("Area of circle with radius %d is: %.2f\n", r, CIRCLE_AREA(r + 1));
return 0;
}
What will be the output of this code?
Correct Answer: Area of circle with radius 4 is: 50.24
Explanation: The `CIRCLE_AREA` macro calculates the area of a circle given its radius. In this case, `CIRCLE_AREA(r + 1)` expands to `(3.14 * (r + 1) * (r + 1))`, resulting in approximately 50.24.