ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
char *s = "programming";
int c = 0;
while (*s)
if (*s++ == 'm') c++;
printf("%d %c", c, *(s - 1));- A. 1 g
- B. 2 m
- C. 2 g
- D. 3 g
Correct answer: C. 2 g
*s++ reads the current character and then advances s, so every character is compared once; "programming" has two 'm's. The loop stops when s points at the terminating '\0', so s − 1 points at the last letter, 'g'. Only s itself moves; the literal is never modified.
More C Programming questions
- What is printed? c unsigned x = 0xF0; / 32-bit unsigned / printf("%u %u %u %u", x >> 4, x << 1, x & 0x3C, x | 0x0F);
- What is printed? c int s = 0, i; for (i = 0; i < 10; i++) { if (i % 3) continue; s += i; } printf("%d %d", s, i);
- What is printed? c struct P { int x, y; } p = {3, 4}, q = &p; q->x += q->y; (q).y = 2; printf("%d %d", p.x, p.y);
- What is printed? c int m[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int s = 0; for (int i = 0; i < 3; i++) s += m[i][2 - i]; printf("%d %d", s…
- What is printed? c int a[5] = {1, 2, 3, 4, 5}; int p = a + 4; while (p > a) printf("%d", --p);
- What is printed? c int tick(int reset) { static int c = 10; if (reset) c = 0; return c += 2; } int main(void) { int a = tick(0); int b =…