ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
int i, j;
for (i = 0, j = 0; i < 5; i++, j += i);
printf("%d %d", i, j);- A. 5 10
- B. 4 10
- C. 5 14
- D. 5 15
Correct answer: D. 5 15
The ; right after the for header is an empty body. The comma operator evaluates left to right, so each step first increments i and then adds the NEW i to j: 1 + 2 + 3 + 4 + 5 = 15. The loop stops with i = 5. 10 would add i before incrementing.
More C Programming questions
- What is printed? c / LP64: 4-byte int, 8-byte long, 4-byte float / char c = 'a'; short s = 2; printf("%zu %zu %zu %zu", sizeof(c)…
- What is printed? c / x86-64 LP64: natural alignment (double 8, int 4, short 2) / struct X { char a[5]; double d; char e; }; struct Y {…
- What is printed? c void p(const char s) { if (!s) return; putchar(s); p(s + 2); putchar(s); } int main(void) { p("abcdef"); }
- What is printed? c / LP64: 4-byte int, 8-byte pointers / int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (p)[3] = a; printf("%d %d %zu %zu"…
- What is printed? c int i, c = 0; for (i = 0; i < 6; i++) if (i % 2 == 0 || ++c > 1) c += 10; printf("%d %d", i, c);
- What is printed? c / 16-bit unsigned short, 32-bit int and unsigned / unsigned short a = 3, b = 5; printf("%d %u %d", a - b, 3u - 5u…