ProgrammingVery hardProduct company OA
What is printed?
What is printed?
const char *s = "";
int n = 0;
for (int i = 0; i < strlen(s) - 1; i++) {
n++;
if (n > 5) break;
}
printf("%d", n);- A. 1
- B. 0
- C. Nothing: it loops forever
- D. 6
Correct answer: D. 6
strlen returns size_t, which is unsigned, so strlen("") − 1 wraps to SIZE_MAX, and i is converted to size_t for the comparison. 0 < SIZE_MAX is true, so only the break ends the loop, with n = 6. The intended '0' needs a signed comparison or i + 1 < strlen(s).
More C Programming questions
- What is printed? c int pm(int b, int e, int m) { if (e == 0) return 1 % m; int h = pm(b, e / 2, m); h = h h % m; return e % 2 ? h b % m…
- What is printed? c #define STR(x) #x printf("%zu", strlen(STR( a + b )));
- What is printed? c char g[3][4] = {"abc", "def", "ghi"}; for (int j = 0; j < 3; j++) putchar(g[2 - j][j]);
- A file report.txt already exists and holds data. What does fopen("report.txt", "w") do?
- The user types hello and presses Enter. After char buf[8]; fgets(buf, sizeof buf, stdin);, what does buf hold?
- The user types abcdefghij and presses Enter. What is strlen(buf) after char buf[8]; fgets(buf, sizeof buf, stdin);?