ProgrammingModerateTCS NQT · Wipro
What is printed?
What is printed?
int n = 3, s = 0;
while (n--) s += n;
printf("%d %d", s, n);- A. 3 0
- B. 6 -1
- C. 3 -1
- D. 6 0
Correct answer: C. 3 -1
The test uses the old value and then decrements, so the body sees 2, 1 and 0 (s = 3). The failing test reads 0 and still decrements, leaving n at −1. '6' adds 3, 2, 1.
More C Programming questions
- Assume ASCII. What is printed? c int cmp(const void a, const void b) { return strcmp((const char )a, (const char )b); } int main(void) {…
- What is printed? c int g[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}}; int mp(int r, int c) { if (r == 0 && c == 0) return g[0][0]; if (r < 0…
- What is printed? c unsigned next(unsigned x) { unsigned c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } int main(void) {…
- What is printed? c const char s = "abcabcab"; const char p = strstr(s, "cab"); printf("%td %s %td", p - s, p + 3, strstr(p + 1, "cab") - s);
- What is printed? c struct A { int v[3]; }; struct A make(void) { struct A a = {{1, 2, 3}}; return a; } int main(void) { struct A x =…
- What is printed? c const char s = "a1b2c3d4"; int sum = 0; for (; s; s++) { if (s >= 'a') continue; if (s == '3') break; sum += s - '0'; }…