ProgrammingHard
char b[4]; int r = snprintf(b, 4, "%d", 123456); printf("%d %s", r, b); What is printed?
- A. 6 123
- B. 3 123
- C. 6 1234
- D. 4 123
Correct answer: A. 6 123
The return value is the length the full output would need, 6, even though only '123' and the terminator fit in the 4-byte buffer.
More C Programming questions
- int pal(const char s) { int i = 0, j = (int)strlen(s) - 1; while (i < j) if (s[i++] != s[j--]) return 0; return 1; } How many of "a"…
- char s[] = "xyz"; for (int i = 0; s[i]; i++) s[i] = (s[i] - 'a' + 3) % 26 + 'a'; printf("%s", s); What is printed?
- 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?
- 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?