ProgrammingVery hardProduct company OA
What is printed?
What is printed?
char s[] = "a,b,,c";
int n = 0;
for (char *t = strtok(s, ","); t; t = strtok(NULL, ","))
n++;
printf("%d %zu %c", n, strlen(s), s[2]);- A. 4 1 b
- B. 3 6 ,
- C. 3 1 b
- D. 4 6 b
Correct answer: C. 3 1 b
strtok treats runs of delimiters as one separator and never returns empty tokens, so the tokens are "a", "b", "c": n = 3. It also writes '\0' over the delimiter that ends each token, so s[1] becomes '\0' and strlen(s) is now 1. s[2] is still 'b'. This is why strtok must never be given a string literal.
More C Programming questions
- What is printed? c char s[] = "Hello"; s[0] = 'J'; s[4] = '0'; printf("%s %zu", s, sizeof(s));
- What is printed? c int i = 5; printf("%d ", i++); printf("%d", ++i);
- What is printed? c printf("%.2f %d %d", 7 / 2.0, 7 / 2, 7 % 2);
- What is printed? c int x = 5; void f(void) { int x = 1; x++; } void g(void) { x++; } int main(void) { f(); g(); g(); printf("%d", x); }
- What is printed? c char s = "programming"; int c = 0; while (s) if (s++ == 'm') c++; printf("%d %c", c, (s - 1));
- What is printed? c unsigned x = 0xF0; / 32-bit unsigned / printf("%u %u %u %u", x >> 4, x << 1, x & 0x3C, x | 0x0F);