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");
  1. A. "0007|xy" and 8
  2. B. "0007|xy" and 7
  3. C. "7|xy" and 4
  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

Exams that ask this topic