What will be the output of the following code when the input is HelloWorld?
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;
}
Show answerHide answer
Correct answer: (A) Input: Hell
Explanation
The width %4s makes scanf read at most 4 characters, so str gets "Hell" plus '\0' (fits in 5 bytes). scanf returns 1, so "Input: Hell" is printed.