I need an authoritative source/documentation for this

S

Sathyaish

Towards the end of this thread here

http://www.codeguru.com/forum/showthread.php?t=293945

a poster named cma said that if we wanted to ignore the input in the
getchar() function, we could place a call to scanf just before the
getchar in this fashion and the input recieved from the getchar will
be ignored (only the next one character):

int temp;
scanf("%*c");
temp=getchar();

I want some source on the Web or some book to explain this * thing to
me and why it behaves that way. I've read K&R, and another book or two
on C but none so far have discussed this behaviour of scanf. I guess
such things are discovered through experience. I'd be grateful to
someone who could point me to some documentation that discusses this
behaviour.
 
B

Ben Pfaff

a poster named cma said that if we wanted to ignore the input in the
getchar() function, we could place a call to scanf just before the
getchar in this fashion and the input recieved from the getchar will
be ignored (only the next one character):

int temp;
scanf("%*c");
temp=getchar();

From the GNU libc manual:

The conversion specifications in a `scanf' template string have the
general form:

% FLAGS WIDTH TYPE CONVERSION

In more detail, an input conversion specification consists of an
initial `%' character followed in sequence by:

* An optional "flag character" `*', which says to ignore the text
read for this specification. When `scanf' finds a conversion
specification that uses this flag, it reads input as directed by
the rest of the conversion specification, but it discards this
input, does not use a pointer argument, and does not increment the
count of successful assignments.

[...]

I'd be surprised if this wasn't in K&R as well.
 
D

Dan Pop

In said:
Towards the end of this thread here

http://www.codeguru.com/forum/showthread.php?t=293945

a poster named cma said that if we wanted to ignore the input in the
getchar() function, we could place a call to scanf just before the
getchar in this fashion and the input recieved from the getchar will
be ignored (only the next one character):

int temp;
scanf("%*c");
temp=getchar();

What's wrong with

getchar(); /* discard the next character from stdin */
temp = getchar();
I want some source on the Web or some book to explain this * thing to
me and why it behaves that way. I've read K&R, and another book or two
on C but none so far have discussed this behaviour of scanf. I guess

Reading books with the brain left in neutral is a mere waste of time.
such things are discovered through experience.

They're properly documented in any good C tutorial, no need to experiment.
I'd be grateful to
someone who could point me to some documentation that discusses this
behaviour.

K&R2, pages 245-246.

Dan
 
T

Tim Hagan

Sathyaish said:
I want some source on the Web or some book to explain this * thing to
me and why it behaves that way. I've read K&R, and another book or two
on C but none so far have discussed this behaviour of scanf.

K&R2 page 245 section B1.3:

"A conversion specification determines the conversion of the next input
field. Normally the result is placed in the variable pointed to by the
corresponding argument. If assignment suppression is indicated by *, as
in %*s, however, the input field is simply skipped; no assignment is
made."
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top