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

Recommended Answers

All 13 Replies

Member Avatar for I_m_rude

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.

IOW, get rid of the \n in the scanf() call.

Member Avatar for I_m_rude

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

sepp2k explained it.

Member Avatar for I_m_rude

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

Because it won't work the way you have it. You cannot end the format string with \n.

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.

Member Avatar for I_m_rude

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.