scanf_s("%d", &d);
This reads characters as long as they fit the "%d"
format. The first character not fitting that format is the newline you entered. This remains in the input stream.
scanf_s("%f", &f);
This skips leading whitespaces (i.e., your newline), then reads characters as long as they fit the "%f"
format. The first character not fitting that format is the newline you entered. This remains in the input stream.
scanf_s("%c", &c);
This reads one character from the input without skipping potential leading whitespace, i.e., your newline.
It also is undefined behaviour: One thing that scanf_s()
does differently than standard scanf()
is that it demands a “buffer size” numerical parameter for %c
and %s
, to avoid buffer overflow. Not giving that parameter makes scanf_s()
pull an integer from the stack where you didn’t put one, resulting in undefined behaviour. If that next memory address happens to be zero, you’ll be scratching your head a lot I would bet…
4
solved in C programming, how to print 1 character [closed]