ProgrammingHardProduct company OA
After this code, what does buf hold and what is n?
After this code, what does buf hold and what is n?
char buf[20]; int n = sprintf(buf, "%04d|%.2s", 7, "xyz");
- A. "0007|xy" and 8
- B. "0007|xy" and 7
- C. "7|xy" and 4
- D. "0007|xyz" and 8
Correct answer: B. "0007|xy" and 7
%04d pads 7 with zeros to width 4; for a string, .2 is a precision that prints at most 2 characters, so "xyz" becomes "xy". sprintf returns the number of characters written, not counting the '\0': 7. '8' counts the terminator.
More C Programming questions
- What is printed? c int c = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (j == i) continue; if (j > i + 1) break; c++…
- Which of the following is NOT allowed in C?
- What is printed? c int cnt(int n, int last) { if (n == 0) return 1; return cnt(n - 1, 0) + (last ? 0 : cnt(n - 1, 1)); } int main(void) {…
- On a two's-complement machine, what is printed? c int a = 5; printf("%d %d %d", !a, ~a & 0xF, !!a + !~-1);
- What is printed? c const char s = "a1b22c333"; int sum = 0, cur = 0; for (; s; s++) { if (s >= '0' && s <= '9') cur = cur 10 + (s - '0')…
- What is printed? c typedef int (Op)(int); int twice(int x) { return 2 x; } int plus3(int x) { return x + 3; } int apply(Op f, Op g, int x)…