By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

getchar can't be used twice?

P: n/a
Hi--

I have the following code:
#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}
But when run, it takes only the input for the first character, then
continues without pausing for the next input:
[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
..
What am I doing wrong?

Best,
Jonathan

Apr 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Jonathan wrote:
I have the following code:

#include <stdio.h>
char a,b;
int main()
{
printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}
But when run, it takes only the input for the first character, then
continues without pausing for the next input:


If the user types '5' and hits return, the first getchar() consumes the
'5', and the 2nd consumes the '\n'. A simple solution is to put an
extra getchar() right after the a=getchar() line to consume the '\n'.

Apr 13 '06 #2

P: n/a
Jonathan <rl******@nospam.us> writes:
I have the following code:
#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}
getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:
[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.
What am I doing wrong?


In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 13 '06 #3

P: n/a

in article ln************@nuthaus.mib.org, Keith Thompson at ks***@mib.org
wrote on 4/13/06 1:52 PM:
Jonathan <rl******@nospam.us> writes:
I have the following code:
#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}


getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:
[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.
What am I doing wrong?


In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

Yike. I tried reading that section and still don't understand this. If it's
the case that getchar is getting the \n as an input, wouldn't that mean that
"a" would be assigned to \n and "b" to 5, not the other way around?

In other words, why is it taking the \n after the a var is input? I don't
see where it's getting that from.

Best,
Jonathan

Apr 13 '06 #4

P: n/a
Jonathan <rl******@nospam.us> writes:
in article ln************@nuthaus.mib.org, Keith Thompson at ks***@mib.org
wrote on 4/13/06 1:52 PM:
Jonathan <rl******@nospam.us> writes:
I have the following code:
#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}


getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:
[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.
What am I doing wrong?


In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

Yike. I tried reading that section and still don't understand this. If it's
the case that getchar is getting the \n as an input, wouldn't that mean that
"a" would be assigned to \n and "b" to 5, not the other way around?

In other words, why is it taking the \n after the a var is input? I don't
see where it's getting that from.


They're just assigned in the order in which you entered them.

When you ran the program, you entered two characters, '5' and a
new-line. Your program called getchar() twice. The first call
returned the first character you entered, '5'; the second returned the
second character you entered, '\n'.

The confusing thing is that input is line-buffered. getchar() doesn't
(normally) read directly from the keyboard; instead, it works on top
of some implementation-specific lower level code. The low level code
reads an entire line of input, storing the '5' and '\n' characters
together in an internal buffer. getchar() grabs successive characters
from that buffer, waiting for more input when the buffer is empty.

If it weren't for this input buffering (and there are ways to turn it
off on some systems), typing the '5' would cause getchar() to return
the value '5' immediately, and you'd see the second prompt *before*
you hit <enter>. The prompts would be interleaved with your input
differently, but you'd still get the same results for the same input;
typing "5<ENTER>" would still store '5' in a and '\n' in b.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 13 '06 #5

P: n/a
Jonathan wrote:


in article ln************@nuthaus.mib.org, Keith Thompson at ks***@mib.org
wrote on 4/13/06 1:52 PM:
Jonathan <rl******@nospam.us> writes:
I have the following code:
#include <stdio.h>

char a,b;

int main()
{

printf("Which character is greater?\n");
printf("Type a single character:");
a=getchar();
printf("Type another character:");
b=getchar();

printf("The first character is %c. The second character is %c.\n", a,
b);

return 0;
}

getchar() returns int, not char, so it can distinguish EOF from any
valud input character value. It doesn't matter much for this simple
program, but you should get into the habit of using it correctly.
But when run, it takes only the input for the first character, then
continues without pausing for the next input:
[Session started at 2006-04-13 13:31:15 -0700.]
Which character is greater?
Type a single character:5
Type another character:The first character is 5. The second character is
.
What am I doing wrong?

In response to the first prompt, you typed two characters, not one:
the '5' and a new-line. You assigned the value '5' to a, and the
value '\n' to b.

Try running the program again, typing "56" followed by a new-line;
you'll get '5' in a and '6' in b.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

Yike. I tried reading that section and still don't understand this. If it's
the case that getchar is getting the \n as an input, wouldn't that mean that
"a" would be assigned to \n and "b" to 5, not the other way around?

In other words, why is it taking the \n after the a var is input? I don't
see where it's getting that from.


What order are you pressing keys in? Do you press return first then 5 or
5 first then return? Do you think getchar should return characters in
the order you enter them or in a different order?
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.