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
#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