Question discussion

What happens when the following code executes?

359227Medium DSSSB TGT Computer Science 0 replies 0 views
0
What happens when the following code executes?
#include <stdio.h>
int main() {
    char buffer[50];
    int n = sprintf(buffer, "Hello, World!");
    printf("%d %s", n, buffer);
    return 0;
}
  • APrints '13 Hello'
  • BPrints '12 Hello'
  • CPrints '12 Hello, World!'
  • DPrints '13 Hello, World!'
Correct answer: D

Explanation

sprintf writes "Hello, World!" (13 characters) into buffer and returns 13, so the output is "13 Hello, World!".

Discussion 0

Nobody has replied yet. Start the discussion!