ProgrammingHardProduct company OA
What is printed?
What is printed?
char s[] = "hello world";
char *p = strchr(s, 'o'), *q = strrchr(s, 'o');
*q = '\0';
printf("%s %zu %td", s, strlen(p), q - p);- A. hello 3 3
- B. hello w 3 3
- C. hello w 7 3
- D. hello world 7 3
Correct answer: B. hello w 3 3
strchr finds the first 'o' (index 4) and strrchr the last (index 7). Writing '\0' at index 7 cuts s to "hello w", and the string from p is now "o w", length 3. q − p = 3. '7' measures p as if the string had not been cut.
More C Programming questions
- What is printed? c unsigned p = 0xB5; p ^= p >> 4; p ^= p >> 2; p ^= p >> 1; printf("%u", p & 1);
- Assume 2-byte short, 4-byte int and natural alignment. What is printed? c #include <stddef.h> struct R { char a; short b; char c; int d…
- What is printed? c int sum(int (r)[3], int n) { int s = 0; for (int i = 0; i < n; i++) s += r[i][i % 3]; return s; } int main(void) { int…
- What is printed? c int even(int n); int odd(int n) { return n == 0 ? 0 : even(n - 1); } int even(int n) { return n == 0 ? 1 : odd(n - 1)…
- What is printed? c char w[] = {"pear", "fig", "apple", "kiwi"}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3 - i; j++) if…
- What is printed? c int score = 73; char g = score >= 90 ? 'A' : score >= 75 ? 'B' : score >= 60 ? 'C' : 'D'; printf("%c%d", g, g - 'A');