Notification texts go here Contact Us Buy Now!

Doesnt \n remain in the buffer?

For most of its conversions, including %d, scanf starts by reading and ignoring initial white-space characters, including the new-line character. Only for the conversions with [, c, or n does it not skip white-space characters.

To tell scanf to skip white-space characters before processing %c, insert a space in the format: scanf(" %c", &b);.

Most scanf formats will read and discard leading white space (e.g. space, tab, and newline).

So, in the shown code, the newline will be left after the first call to scanf. But the second call to scanf will read and discard it. However, the newline after that input will be left in the input buffer.

The only three scanf formats that do not read and discard leading white space are %c, %[…] (scan sets) and %n. If you want the same behavior for those formats, you need to add an explicit leading space in the format string yourself.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.