Question discussion

What will be the output of the following code when the input is HelloWorld?

359031Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the following code when the input is HelloWorld?
#include <stdio.h>
int main() {
    char str[5]; // Buffer for input
    int result = scanf("%4s", str);
    if (result == 1) {
        printf("Input: %s\n", str);
    } else {
        printf("Input error or no input.\n");
    }
    return 0;
}
  • AInput: Hell
  • BInput: Hello
  • CInput error or no input
  • DInput: HelloWorld
Correct answer: A

Explanation

scanf("%4s") reads at most 4 characters, so only "Hell" is stored (plus the null terminator in the 5-element buffer) and printed.

Discussion 0

Nobody has replied yet. Start the discussion!