ProgrammingVery hard
char d[8] = "#######"; strncpy(d, "ab", 5); int z = 0; for (int i = 0; i < 8; i++) if (d[i] == 0) z++; printf("%d", z); What is printed?
- A. 3
- B. 5
- C. 6
- D. 4
Correct answer: D. 4
strncpy copies 'ab' and pads the destination with zeros up to 5 bytes, so positions 2 to 4 become zero. Position 5 and 6 keep their '#', and position 7 was already the terminator.
More C Programming questions
- int a = 0, b = 0; char op = 0; int n = sscanf("12 + x", "%d %c %d", &a, &op, &b); printf("%d %c", n, op); What is printed?
- Which loop is safe for every value a plain char can hold, including bytes above 127 that may be negative?
- What is the smallest array size that holds the string "Ravi" with strcpy and no undefined behaviour?
- char s[] = "x41x420x43"; printf("%zu", strlen(s)); What is printed?
- char s[] = "banana"; char p = strrchr(s, 'a'); printf("%ld", (long)(p - s)); What is printed?
- printf("%zu", strcspn("aXbYc", "XY") + strspn(" 42ab", " 0123456789")); What is printed?