ProgrammingModerate
char d[4]; strncpy(d, "abcdef", 4); /* d now has no terminator */ Which call is safe to print d with?
- A. printf("%s", d);
- B. puts(d);
- C. printf("%.4s", d);
- D. printf("%zu", strlen(d));
Correct answer: C. printf("%.4s", d);
strncpy with n equal to the array size writes no terminator, so %s and strlen run past the array. A precision of 4 prints exactly the four bytes.
More C Programming questions
- char s[] = "x=1;y=2"; char t = strtok(s, "="); printf("%s %zu", t, strlen(s)); What is printed?
- Given char names[] holding strings, which qsort comparator sorts them alphabetically?
- char d[6] = "abc"; / we want strncat(d, "defgh", 5) with no overflow / What is the smallest size d needs?
- char b[16]; sprintf(b, "%d%%", 50); printf("%zu", strlen(b)); What is printed?
- printf("%c", 321); What is printed?
- char a[20], b[20]; / stdin: "hi therennextn" / scanf("%s", a); fgets(b, sizeof b, stdin); What is stored in b?