ProgrammingVery hardProduct company OA
What is printed?
What is printed?
#define SQ(x) x * x
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int calls = 0;
int next(void) { return ++calls; }
int main(void) {
int a = MIN(next(), 5);
int b = SQ(a + 1) / SQ(2);
printf("%d %d %d", a, b, calls);
}- A. 1 1 1
- B. 2 2 2
- C. 2 4 2
- D. 1 4 1
Correct answer: C. 2 4 2
Macros substitute text. MIN(next(), 5) becomes ((next()) < (5) ? (next()) : (5)): the first call (1) is only compared; the selected operand calls next() AGAIN, so a = 2 and calls = 2 (well defined – ?: has a sequence point). SQ has no parentheses: a + 1 * a + 1 / 2 * 2 = 2 + 2 + 0 = 4, because 1 / 2 is 0 in integer division. '2 2 2' assumes SQ(a + 1) / SQ(2) = 9 / 4.
More C Programming questions
- What is printed? c #define N 4 #define STR(x) #x #define XSTR(x) STR(x) #define CAT(a, b) a##b #define XCAT(a, b) CAT(a, b) int NN = 9…
- Which of the numbered statements violate a constraint (i.e. must be rejected by the compiler)? c typedef char strt; #define STRM char char…
- What is printed? c int g = 1; int f(int n) { static int s = 10; int g = n; s += g; { extern int g; g += s; } return s; } int main(void) {…
- What is printed? c int i = 0, j = 5, k = 0; int r = (i++ || j++) && (k++, i + j > 1) ? i++ : (j += 2, k); printf("%d %d %d %d", r, i, j, k);
- What is printed? c const char w[] = {"heap", "stack", "queue", "trie"}; const char p = w; printf("%c", ++p); printf("%c", ++p)…
- Assume 8-bit unsigned char. What is printed? c unsigned int count = 0; for (unsigned char i = 250; i != 4; i += 3) count++; printf("%u"…