ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
const char *s = "mississippi";
printf("%td %td %td", strchr(s, 's') - s, strrchr(s, 's') - s, strstr(s, "ssip") - s);- A. 2 6 2
- B. 2 6 5
- C. 3 7 6
- D. 2 3 5
Correct answer: B. 2 6 5
Indices: m0 i1 s2 s3 i4 s5 s6 i7 p8 p9 i10. strchr finds the FIRST 's' (index 2) and strrchr the LAST (index 6). strstr returns a pointer to the first occurrence of the whole substring "ssip", which begins at index 5 ("ssippi"); the earlier "ss" at 2 is followed by 'i','s', not 'i','p'. Subtracting s turns each pointer into an index.
More C Programming questions
- What is printed? c / 4-byte int / struct V { int a[3]; }; void f(struct V v) { v.a[0] = 9; } void g(int a[3]) { a[0] = 9; } int main(void)…
- What is printed? c int n = 10, c = 0; int k = (n + 3) / 4; switch (n % 4) { case 0: do { c++; case 3: c++; case 2: c++; case 1: c++; }…
- What is printed? c int calls = 0; int ways(int n) { calls++; if (n < 0) return 0; if (n == 0) return 1; return ways(n - 1) + ways(n - 3) +…
- What is printed? c int moves = 0; void h(int n, char from, char via, char to) { if (n == 0) return; h(n - 1, from, to, via); if (++moves…
- What is printed? c for (unsigned n = 5; n < 8; n++) printf("%u ", n ^ (n >> 1)); unsigned g = 13, b = 0; for (; g; g >>= 1) b ^= g…
- What is printed? c int walk(int n) { static int calls = 0; calls++; if (n > 1) { walk(n / 2); walk(n / 3); } return calls; } int…