472,958 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 6482
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.