int n;
scanf("%d\n", &n);
printf("you typed %d\n", n);

it seems to hang until I type one extra line of input....Can anyone explain this behaviour

I don't think so there should be any problem. But try to give the code in which you are facing this problem, then that will be easier to judge where is the problem. thanks.

A whitespace character (including \n) in the format string will cause scanf to discard any whitespace in the input stream upto the next token. If a whitespace character is the last character in the format string that means that the user must already enter the next token, so that scanf knows upto where it needs to discard whitespace.

Genereally you simply don't want to have whitespace characters at the end of your format string.

sir, will you please explain it more that how '\n' is making problem here. please explain it . thanks.

but, i didn't get it na... plzz it's a request to u .. please

You cannot end the format string with \n

Well, you can. It just means that scanf won't return until the user has entered another token. (Not that I could imagine a scenario where that's ever useful).

</pedantry>

Quoted Text Here

It just means that scanf won't return until the user has entered another token

does this mean that the user needs to enter another input (may be a string or an integer or so) so that the scanf returns?

does this mean that the user needs to enter another input (may be a string or an integer or so) so that the scanf returns?

It means there needs to be at least one non-whitespace character after all of the whitespace characters. When you put whitespace in the format string, you can think of it as a specifier saying "read and discard all contiguous whitespace". If a newline is the last character then scanf() doesn't have any way of knowing it's the last and will wait for you to give it more.

@jame sir, but why is it so ? i mean why scanf is waiting for input ? why is it expecting a character ?

and secondly , new line is a white space or non-white space character ? i am confused!

thanks to you.

i mean why scanf is waiting for input ? why is it expecting a character ?

Because, as has been explained repeatedly now, a white space in scanf's format string means "consume any whitespace characters until you find the next non-whitespace character". Why does it mean that? Because the C standard says that that's what it means.

new line is a white space or non-white space character ?

The newline character is a whitespace character. For more information on what is and isn't a whitespace character see the documentation of the isspace function.

For more information on what is and isn't a whitespace character see the documentation of the isspace function.

Or better yet, write a quick program to loop from 0 to 255 and call all the isXXX() functions and print the results in a chart:

char isspace isdigit isalpha ispunct ....
0x00    F       F       F      F
...
0x20    T       F       F      F
...

Then you can have a list at hand if you ever have doubts.

This article has been dead for over six months. Start a new discussion instead.