question on assignment suppression in scanf

S

subramanian100in

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
 
M

Michael

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

Ben Bacarisse

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top