ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
#define IS_POSITIVE(x) ((x) > 0)
#define IS_NEGATIVE(x) ((x) < 0)
#define IS_ZERO(x) ((x) == 0)
int classify(int n) {
if (IS_POSITIVE(n)) return 1;
if (IS_NEGATIVE(n)) return -1;
return 0;
}
int main(void) {
printf("%d %d %d", classify(5), classify(-3), classify(0));
}- A. 1 -1 0
- B. 1 1 0
- C. -1 1 0
- D. 1 -1 1
Correct answer: A. 1 -1 0
Each macro expands to a simple, fully-parenthesized boolean test, avoiding any precedence traps. classify(5): 5>0 true, returns 1. classify(-3): -3>0 false, -3<0 true, returns -1. classify(0): neither positive nor negative, falls through to return 0.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int findMax(int arr, int n) { int maxPtr = arr; for (int i = 1; i < n; i++) if (arr[i] >…
- Assume a 64-bit LP64 ABI. What is printed? c int binarySearch(int arr, int n, int target) { int lo = 0, hi = n - 1; while (lo <= hi) { int…
- Assume a 64-bit LP64 ABI. What is printed? c int sumArray(int arr, int n) { if (n == 0) return 0; return arr + sumArray(arr + 1, n - 1); }…
- Assume a 64-bit LP64 ABI. What is printed? c int compareInts(const void a, const void b) { int ia = (const int )a; int ib = (const int )b…
- Assume a 64-bit LP64 ABI. What is printed? c int fibIterative(int n) { int a = 0, b = 1; for (int i = 0; i < n; i++) { int tmp = a + b; a…
- Assume a 64-bit LP64 ABI. What is printed? c int filterEven(int arr, int n, int outCount) { static int result[10]; int count = 0; for (int…