hopeless scanf (or I'm hopeless :))

H

Horst Kraemer

Hi!
I have the following instruction:
char wyp;
scanf("%c",&wyp);

this scanf is in a while loop, and I want it to get EXACTLY one character
each time.
Does anyone knows how to do it?

scanf("%c",&wyp);

will read one character from stdin and store it to wyp. If a read
error occurred or the end of file is reached scanf will return 0 and
nothing will be stored to wyp.

What is your problem and why are you hopeless?
Maybe someone has a different idea to read excatly one character from
keyboard, not necessarily using scanf.

int c:

c = getchar();

if (c==EOF) { /* Either the end of file is reached or a read error
occured */ }

reads one character from stdin. If the end of file is reached or a
read error occured, getchar returns EOF (this is *not* a character).
 
K

Kris

Hi!
I have the following instruction:
char wyp;
scanf("%c",&wyp);

this scanf is in a while loop, and I want it to get EXACTLY one character
each time.
Does anyone knows how to do it?
BTW. How can I get information how many characters were entered and assigned
to variable wyp?

Maybe someone has a different idea to read excatly one character from
keyboard, not necessarily using scanf.

Thanks in advance for your ideas,

Regards,
Chris Rutczynsky
 
K

Kevin Goodsell

Kris said:
Hi!
I have the following instruction:
char wyp;
scanf("%c",&wyp);

That's a statement. There are no "instructions" in C.
this scanf is in a while loop, and I want it to get EXACTLY one character
each time.

That seems to be what your scanf statement will do.
Does anyone knows how to do it?
BTW. How can I get information how many characters were entered and assigned
to variable wyp?

The return value from scanf will tell you how many items were converted
and assigned. This will always be 1 in the example above (unless an
error occurs).

wyp is a single char variable - it can only hold one character at a
time. Count how many times the scanf is successfully executed, and that
will tell you how many characters have been assigned to wyp. Note that
only the most recent character is still there.
Maybe someone has a different idea to read excatly one character from
keyboard, not necessarily using scanf.

The C language does not support the concept of keyboards. All I/O is in
terms of streams. Input is line-buffered, so the program won't see your
keyboard input until you press Enter. In other words, there is no
standard way to read a single keystroke (unless the keystroke happens to
be Enter).

-Kevin
 
K

Kevin Goodsell

Horst said:
scanf("%c",&wyp);

will read one character from stdin and store it to wyp. If a read
error occurred or the end of file is reached scanf will return 0 and
nothing will be stored to wyp.

I believe it will return EOF if end of file is reached.

-Kevin
 
H

Horst Kraemer

I believe it will return EOF if end of file is reached.

Sorry, thank you.

To the OP:

int result;
int wpc;
while (...)
{
result=scanf("%c",&wyp);
if (result==EOF) { /* end of file or read error */ }
}

will try to read one character from stdin and store it to wyp. If a
read error occurred or the end of file is reached before reading scanf
will return EOF and nothing will be stored to wyp, i.e. result will
get the value of the macro EOF.

This is equivalent to and slighty more complicated than

int c;

while(...)
{
c = getchar();
if (c==EOF) { /* end of file or error */ }
}

where in case of error/eof the EOF result is stored in the same
variable where a character would be stored if the function sucessfully
returns a character. Note that the variable which will contain the
character read or the value EOF *must* be declared as int and not as
char because EOF is *not* a character.
 
A

Anuj Heer

The C language does not support the concept of keyboards. All I/O is in
terms of streams. Input is line-buffered, so the program won't see your
keyboard input until you press Enter. In other words, there is no
standard way to read a single keystroke (unless the keystroke happens to
be Enter).

-Kevin

well kevin there is a way in c whereby you can detect single
keystrokes..

char c;
while(!kbhit())
{

}
c = getch();

this will move forward on any keystroke and at the same time saveit in
char c...alternately u can write a interrupt capture routine for the
keyboard isr using getvect and setvect
anuj
(e-mail address removed)
 
S

Sidney Cadot

Anuj said:
well kevin there is a way in c whereby you can detect single
keystrokes..

Not in standard C, there isn't.
char c;
while(!kbhit())
{

}
c = getch();
Ahhhh...!

this will move forward on any keystroke and at the same time saveit in
char c...alternately u can write a interrupt capture routine for the
keyboard isr using getvect and setvect

Your solution seems to invoke functions from a non-standard, platform
dependent library, as would your suggested alternative. That's cheating
a bit, wouldn't you say?

Please realize that all the world is not an Intel based computer running
an operating system originating from an unnamed company based in
Redmond, USA.

Best regards,

Sidney
 
K

Keith Thompson

well kevin there is a way in c whereby you can detect single
keystrokes..

char c;
while(!kbhit())
{

}
c = getch();

this will move forward on any keystroke and at the same time saveit in
char c...alternately u can write a interrupt capture routine for the
keyboard isr using getvect and setvect

The kbhit() and getch() functions are non-standard and extremely
non-portable.

The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>; see
questions 19.1 and 19.2.
 
K

Kevin Goodsell

Anuj said:
well kevin there is a way in c whereby you can detect single
keystrokes..

Wow, that's an amazing revelation. Please explain how I can enter a
keystroke into a program running on my toaster (which of course has no
keyboard), so that I can use this solution there.
char c;
while(!kbhit())
{

}
c = getch();

Also, please supply (standard, portable) definitions for kbhit() and
getch() so that I can use them.

-Kevin
 
E

Ekkehard Morgenstern

char wyp;
scanf("%c",&wyp);

careful! scanf( "%c" ) reads an integer and stores it in char, overwriting
three bytes on the stack before the wyp declaration (possibly the return
address and your program crashes).

correct would be:

int wyp;
scanf( "%c", &wyp );

all of the scanf() family functions read over whitespace. Might be that the
%c is spared, but like the others said, if you want to be sure to read
single characters, just use getchar().

int wyp = getchar();

:)
 
K

Kevin Goodsell

Ekkehard said:
careful! scanf( "%c" ) reads an integer and stores it in char, overwriting
three bytes on the stack before the wyp declaration (possibly the return
address and your program crashes).

correct would be:

int wyp;
scanf( "%c", &wyp );

Everything in this post so far is absolutely false, and the code above
exhibits undefined behavior. %c matches a char*. With no field width it
reads and stores a single char. No additional null character is stored,
so the pointer need only point to a single char.
all of the scanf() family functions read over whitespace.

%c, %[, and %n are not subject to that rule.
Might be that the
%c is spared, but like the others said, if you want to be sure to read
single characters, just use getchar().

int wyp = getchar();

-Kevin
 
D

Dan Pop

careful! scanf( "%c" ) reads an integer and stores it in char, overwriting
three bytes on the stack before the wyp declaration (possibly the return
address and your program crashes).

correct would be:

int wyp;
scanf( "%c", &wyp );

Please don't post bullshit to this newsgroup. Either read a C book or
get lost!
all of the scanf() family functions read over whitespace. Might be that the
%c is spared,

There is no place for "maybe" on such issues. The way scanf handles %c
is perfectly well defined and if you can't be bothered to check it, you
have no business posting to this thread.
but like the others said, if you want to be sure to read
single characters, just use getchar().

int wyp = getchar();

When parsing a line, it is not unusual to want to read single characters
between other fields:

int rc = sscanf(line, "%40[^;, ]%c %40[^\n]%c", first, &sep, last, &nl);

Dan
 
E

Ekkehard Morgenstern

Kevin Goodsell said:
Everything in this post so far is absolutely false, and the code above
exhibits undefined behavior. %c matches a char*. With no field width it
reads and stores a single char. No additional null character is stored,
so the pointer need only point to a single char.

sorry it was late, and I thought I was right (of course! ;-) ) . I looked it
up in an ANSI C book.

It's logical too, otherwise "%34c" would make no sense.
all of the scanf() family functions read over whitespace.

%c, %[, and %n are not subject to that rule.
true.
int wyp = getchar();

however, THIS is correct! ;-)
 
E

Ekkehard Morgenstern

Dan Pop said:
Please don't post bullshit to this newsgroup. Either read a C book or
get lost!

I didn't look it up beforehand, sorry.
There is no place for "maybe" on such issues. The way scanf handles %c
is perfectly well defined and if you can't be bothered to check it, you
have no business posting to this thread.

Freedom of speech, brother! ;-)

The right to err! ;-)

It was late and I thought it'd be correct, perhaps I shouldn't post anything
when I'm tired, lol.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top