ProgrammingModerateAccenture · Capgemini
What is printed?
What is printed?
int f(void) { static int n = 5; return n--; }
int main(void) {
int s = 0;
while (f() > 2) s++;
printf("%d %d", s, f());
}- A. 4 1
- B. 3 2
- C. 2 1
- D. 3 1
Correct answer: D. 3 1
The static n keeps its value across calls and f returns the old value. The loop tests 5, 4, 3 (true, s becomes 3) and then 2 (false, n is now 1). The call inside printf returns 1. '3 2' forgets that the failing test also called f.
More C Programming questions
- What is printed? c #define MUL(a, b) a b #define ADD(a, b) a + b printf("%d", MUL(ADD(1, 2), ADD(3, 4)));
- Assume little-endian, 8-bit char and 32-bit unsigned int. What is printed? c union { unsigned int u; unsigned char b[4]; } x; x.u =…
- What is printed? c int a = 6, b = 2; int r = a & 3 << b | 1; printf("%d", r);
- What is printed? c int i = 1, j = 2, k = 3; int r = (i > j) ? i++ : j > k ? j++ : (k++, i + j + k); printf("%d %d %d %d", i, j, k, r);
- What is printed by r("ABCDE", 0)? c void r(const char s, int d) { if (!s) return; if (d % 2) putchar(s); r(s + 1, d + 1); if (d % 2 == 0)…
- What is printed? c int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int (r)[3] = m + 1; printf("%d %d %d", r[1][0], (r)[2], (r - 1) +…