ProgrammingVery hardProduct company OA · GATE CS
With int a = 3, b = 7; which of these statements compile as C (not C++)?
With int a = 3, b = 7; which of these statements compile as C (not C++)?
*(a > b ? &a : &b) = 0; /* (i) */ (a > b ? a : b) = 0; /* (ii) */ a > b ? a : b = 0; /* (iii) */ a > b ? (a = 0) : (b = 0); /* (iv) */
- A. (i) and (iv) only
- B. (i), (iii) and (iv) only
- C. All four
- D. (i) only
Correct answer: A. (i) and (iv) only
In C the result of ?: is never an lvalue, so (ii) cannot be assigned to. C's grammar also parses (iii) as (a > b ? a : b) = 0, so it fails the same way; only C++ reads it as a > b ? a : (b = 0). (i) picks an address and dereferences it, and (iv) just puts assignments inside the branches, so both compile.
More C Programming questions
- What is printed? c int f(int n) { static int depth = 0; depth++; if (n <= 1) return depth; int a = f(n - 1); int b = f(n - 2); return a +…
- What is printed? c char s[] = "hello world"; char p = strchr(s, 'o'), q = strrchr(s, 'o'); q = '0'; printf("%s %zu %td", s, strlen(p), q…
- What is printed? c unsigned p = 0xB5; p ^= p >> 4; p ^= p >> 2; p ^= p >> 1; printf("%u", p & 1);
- Assume 2-byte short, 4-byte int and natural alignment. What is printed? c #include <stddef.h> struct R { char a; short b; char c; int d…
- What is printed? c int sum(int (r)[3], int n) { int s = 0; for (int i = 0; i < n; i++) s += r[i][i % 3]; return s; } int main(void) { int…
- What is printed? c int even(int n); int odd(int n) { return n == 0 ? 0 : even(n - 1); } int even(int n) { return n == 0 ? 1 : odd(n - 1)…