468,771 Members | 1,879 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

question on assignment suppression in scanf

Consider the following program named as x.c

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
unsigned int u;
char str[1024];

printf("Enter an unsigned integer: ");
scanf("%u", &u);
scanf("%*[^\n]");
getchar();

printf("Enter a string: ");
fgets(str, sizeof str, stdin);

return EXIT_SUCCESS;
}

I compiled the above program with gcc 3.4.3 as
gcc -ansi -pedantic -Wall -Wextra x.c

When I run this program with 10 as the input for unsigned integer, it
is stored into the variable 'u'. Now, only the newline character will
remain in the input buffer. For the second scanf("*[^\n]"), there is
no character left in the input buffer and it DOESN'T WAIT for any
character to be entered from the keyboard. But the subsequent
getchar() function will read and discard the newline character that
was left in the input buffer. So, the fgets will wait for a line to be
entered.

In the above program, if I have the additional line
scanf("%*[\n]");

before the code fragment
printf("Enter an unsigned integer: ");
scanf("%u", &u);
scanf("%*[^\n]");
getchar();

then, this new first scanf("%*[\n]") WAITS for some input to be
entered before going to the printf statement.

My question is why does scanf("%*[^\n]"); wait for some input to
entered when it is kept as the first statement and why scanf("%*[^
\n]"); doesn't wait for input when it is kept after scanf("%u", &u); ?

Also, is the combination of statements
scanf("%*[^\n]");
getchar();
correct to discard all characters including newline character which
are remaining in the input buffer ? Or is there some better way to
accomplish this ?

Another question is, can I combine the statements
scanf("%u", &u);
scanf("%*[^\n]");
into the single statement
scanf("%u%*[^\n]", &u);

Kindly explain.

Thanks
V.Subramanian
Oct 18 '08 #1
2 6098
su**************@yahoo.com, India wrote:
Consider the following program named as x.c

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
unsigned int u;
char str[1024];

printf("Enter an unsigned integer: ");
scanf("%u", &u);
scanf("%*[^\n]");
getchar();

printf("Enter a string: ");
fgets(str, sizeof str, stdin);

return EXIT_SUCCESS;
}

I compiled the above program with gcc 3.4.3 as
gcc -ansi -pedantic -Wall -Wextra x.c

When I run this program with 10 as the input for unsigned integer, it
is stored into the variable 'u'. Now, only the newline character will
remain in the input buffer. For the second scanf("*[^\n]"), there is
no character left in the input buffer and it DOESN'T WAIT for any
character to be entered from the keyboard. But the subsequent
getchar() function will read and discard the newline character that
was left in the input buffer. So, the fgets will wait for a line to be
entered.

In the above program, if I have the additional line
scanf("%*[\n]");

before the code fragment
printf("Enter an unsigned integer: ");
scanf("%u", &u);
scanf("%*[^\n]");
getchar();

then, this new first scanf("%*[\n]") WAITS for some input to be
entered before going to the printf statement.

My question is why does scanf("%*[^\n]"); wait for some input to
entered when it is kept as the first statement and why scanf("%*[^
\n]"); doesn't wait for input when it is kept after scanf("%u", &u); ?

Also, is the combination of statements
scanf("%*[^\n]");
getchar();
correct to discard all characters including newline character which
are remaining in the input buffer ? Or is there some better way to
accomplish this ?

Another question is, can I combine the statements
scanf("%u", &u);
scanf("%*[^\n]");
into the single statement
scanf("%u%*[^\n]", &u);

Kindly explain.

Thanks
V.Subramanian
When running
scanf("%*[\n]");

Because there is no character left in the input stream, it waits for
something to happen but when running

scanf("%*[^\n]");
There is a \n character left in he input stream so it exits without
reading in anything.
Oct 18 '08 #2
"su**************@yahoo.com, India" <su**************@yahoo.com>
writes:
Consider the following program named as x.c

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
unsigned int u;
char str[1024];

printf("Enter an unsigned integer: ");
scanf("%u", &u);
scanf("%*[^\n]");
getchar();

printf("Enter a string: ");
fgets(str, sizeof str, stdin);

return EXIT_SUCCESS;
}

I compiled the above program with gcc 3.4.3 as
gcc -ansi -pedantic -Wall -Wextra x.c

When I run this program with 10 as the input for unsigned integer, it
is stored into the variable 'u'. Now, only the newline character will
remain in the input buffer. For the second scanf("*[^\n]"), there is
no character left in the input buffer and it DOESN'T WAIT for any
character to be entered from the keyboard.
Here is the heart of your confusion, I think. You say "the newline
character will remain in the input buffer" and then "there is no
character left in the input buffer". Both can't be true.

The first is the true statement. There is (at least) a \n in the
buffer and the format "%[^\n]" skips anything that is not \n --
including zero characters.
But the subsequent
getchar() function will read and discard the newline character that
was left in the input buffer. So, the fgets will wait for a line to be
entered.
Which is how I'd want the program to behave, by the way.
In the above program, if I have the additional line
scanf("%*[\n]");

before the code fragment
printf("Enter an unsigned integer: ");
scanf("%u", &u);
scanf("%*[^\n]");
getchar();

then, this new first scanf("%*[\n]") WAITS for some input to be
entered before going to the printf statement.
Yes, it has to wait. All scanf formats require some input to work
on. "%*[\n]" will either skip over a newline or the input will fail
(if the next character is not a newline) but until some input is
available scanf can't tell which it must do.

--
Ben.
Oct 18 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Henry | last post: by
24 posts views Thread by Tweaxor | last post: by
4 posts views Thread by Cal Lidderdale | last post: by
reply views Thread by happy | last post: by
reply views Thread by happy | last post: by
1 post views Thread by siliconwafer | last post: by
7 posts views Thread by bobozelda | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.