Q. What will be the output of the following C code? (If the input is "Hello world")

Code:
#include <stdio.h>

int main()
{
    char str[30];

    scanf("%s", str);
    printf("%s", str);

    return 0;
}
  • (A) Hello world
  • (B) Hello
  • (C) Hello world\0
  • (D) Error
πŸ’¬ Discuss
βœ… Correct Answer: (B) Hello
Explanation: When we read a string using the scanf() function, the input is terminated by the whitespace. Here, the input is "Hello world", so only "Hello" will be stored to str. Thus, the output will be "Hello".
Explanation by: Chandani
When we read a string using the scanf() function, the input is terminated by the whitespace. Here, the input is "Hello world", so only "Hello" will be stored to str. Thus, the output will be "Hello".

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
324
Total Visits
πŸ“½οΈ
3 y ago
Published
πŸŽ–οΈ
Chandani
Publisher
πŸ“ˆ
90%
Success Rate