ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
#define SIGN(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
int main(void) {
int values[3] = {-5, 0, 7};
for (int i = 0; i < 3; i++) printf("%d ", SIGN(values[i]));
}- A. -1 0 1
- B. 1 0 -1
- C. 0 0 0
- D. -5 0 7
Correct answer: A. -1 0 1
SIGN(x) fully parenthesizes its argument everywhere it's used, avoiding precedence traps, so each use is safe even with a complex expression like values[i]. For -5: negative, gives -1. For 0: neither positive nor negative, gives 0. For 7: positive, gives 1.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct Student { char name[10]; int marks[3]; }; struct Student s = {"Bob", {80, 90, 70}}…
- Assume 32-bit int. What is printed? c int x = 1; int y = 2; int z = 3; int result = x + y == z; printf("%d", result);
- What happens when this runs? c int divide(int result, int a, int b) { if (b == 0) return -1; result = a / b; return 0; } int main(void) {…
- Assume 32-bit int. What is printed? c int x = 3; int result = x++ + x++ - x++; printf("%d", result); printf(" valid: %s", "no, undefined…
- What is printed? c int main(void) { int a[3] = {1, 2, 3}; int b[3] = {1, 2, 3}; printf("%d", a == b); }
- What is printed? c int main(void) { char a[] = "test"; char b[] = "test"; printf("%d %d", a == b, strcmp(a, b) == 0); }