Consider following 2 codes inside main function.👇👇👇 why output is different in both the cases???? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Consider following 2 codes inside main function.👇👇👇 why output is different in both the cases????

char arr[50],ch; scanf("%s",arr); scanf ("%c",&ch); printf("%s %c",arr,ch); or char arr[50],ch; scanf("%s %c",arr,&ch); printf("%s %c",arr,ch); Output of 1st is = arr Output of 2nd is=arr ch

18th Jun 2020, 6:36 AM
saurabh
saurabh - avatar
5 Answers
+ 3
char arr[50],ch; scanf("%s",arr); scanf ("%c",&ch); //here the remaining \n new line character is assigned to ch. How to avoid this: char arr[50],ch; scanf("%s\n",arr); scanf ("%c",&ch); That should be equivalent to your second code.
18th Jun 2020, 6:59 AM
Kevin ★
+ 3
This is one of the many problems with scanf(). You should avoid the scanf() functions whenever you can, and use fgets() and getchar() instead.
18th Jun 2020, 11:17 AM
Gen2oo
Gen2oo - avatar
+ 2
due to white space in the buffer memory. Second scanf scans the white space and prints the whitespace. char arr[50],ch; scanf("%s",arr); scanf(" %c",&ch); printf("%s %c",arr,ch); Will work like second one because space before %c will skip the white space in buffer memory
18th Jun 2020, 7:29 AM
Sameer Crestha
Sameer Crestha - avatar
+ 2
18th Jun 2020, 7:49 AM
saurabh
saurabh - avatar
0
Sameer Crestha please see again que.. and then Kevin's answer......
18th Jun 2020, 7:50 AM
saurabh
saurabh - avatar