ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
const char *s = "banana", *p = s;
int c = 0;
while ((p = strchr(p, 'a')) != NULL) { c++; p++; }
printf("%d", c);- A. 3
- B. Nothing: it loops forever
- C. 2
- D. 6
Correct answer: A. 3
strchr returns a pointer to the next 'a', and p++ steps past it so the next search starts after it. Three a's give 3. Without the p++, strchr would keep finding the same 'a' forever.
More C Programming questions
- Assume IEEE-754 double. What is printed? c printf("%d %d", 0.1 + 0.2 == 0.3, 0.5 + 0.25 == 0.75);
- Assume 4-byte int. What is printed? c printf("%d %d", sizeof(int) -1 < 0, (int)sizeof(int) -1 < 0);
- What is printed? c Bool b = 0.5; printf("%d %d", b + 2, (Bool)256);
- What is printed? c int c = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { if (j == 1) break; c++; } printf("%d", c);
- With 4-byte int, what is printed? c int a[4] = {1, 2, 3, 4}; char c = (char )a; int p = (int )(c + 8); printf("%d %td", p, p - a);
- What does Staticassert(sizeof(long) >= 8, "need 64-bit long"); do in C11?