C standard input output Problem

gdf

New Member
Joined
Mar 1, 2022
Messages
29
Reaction score
2
Credits
287
hello!

considering these lines:
char input[100]
printf("len of hello:%ld\n",strlen("hello"));
printf("input a line:");
scanf("%s",input);

What's bothering me is that I have to input one line first ,then it prints the hint line "input a line",I do not know reasons.

please help me!

Thank you!
 


Appreciate for you response.
So do I need fflush every time I want to input something closely after an output line?
 
Also be aware that use of scanf should be avoided, because it will happily overflow your input array.
You’ve given it a maximum size of 100, which would allow it to hold 99 characters, plus a terminating null at the end.

If the user enters more than 99 characters, scanf will simply write past the end of your array, corrupting your memory. Which is extremely bad!

It’s better to use fgets, because you can put a maximum limit on the number of bytes/characters to read from stdin. That way, you can avoid a serious buffer overflow bug!
 

Staff online

Members online


Top