473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6522
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Henry | last post by:
I was doing this program for an exercise in a book. The point was to create a program that would take a numerical grade from a user and convert it to a letter grade (yeah really easy). I tried to...
24
by: Tweaxor | last post by:
This has been puzzling me all this week. This is actually a homework assignment from last semesmter. But the teacher wouldn't tell us why certain things didn't work, but it didn't just work. My...
4
by: Cal Lidderdale | last post by:
My input line is i1,i2,i3,i4,i5,i6,i7,i8^,...i596,597, ... 14101,14102...NL/CR very long line of data - I only want the first 8 items and the delimiter between 8 & 9 is a carrot "^". The line...
0
by: happy | last post by:
That request of having sorting program for that setup programs was not a request to an assignment but to have good ideas about sorting record .. #include <stdio.h> #include <conio.h> struct...
0
by: happy | last post by:
That request of having sorting program for such setup programs was not a request to an assignment but to have good ideas about sorting program .. #include <stdio.h> #include <conio.h> struct...
2
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
1
momotaro
by: momotaro | last post by:
You are to write a card catalog type definition. There are up to 1000 books, and for each one you need: Title A list of up to 5 authors Catalog number A list of up to 5 subject headings Status...
7
by: bobozelda | last post by:
I have an assignment where I am prompted to enter a number and the program will figure the tax on the amount entered. This will be for 3 separate store locations. I finally have the program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.