473,473 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

getchar can't be used twice?

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
5 8694

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
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

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

Similar topics

3
by: Cam | last post by:
Hi everyone, Before I answer to a (hopefully) helpful reply to this post, I have been rapped over the knuckles for 'top-posting' and I do not wish to be a learner poster who observes poor...
6
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always...
17
by: Martin Jørgensen | last post by:
Hi, Since I'm a newbie I have some small but quick (probably) stupid questions also :-) This is my "get_double" function which takes a default argument also of type double. The function...
20
by: blufox | last post by:
This is a simple code snippet i used ... #include <stdio.h> int main(void) { int ch; do { printf("still yes...\n"); printf("\nenter choice :: "); ch = getchar(); }while(ch == 'y');
5
by: CDROOM | last post by:
i have this qussion how getchar used , where it used and when it used. please reply quickly,
25
by: ehabaziz2001 | last post by:
Why I can not begin my subscript of character arrrays with 0. In this program I can not do : do { na=getchar(); i++; na=getchar(); } while (na!='\n');
14
by: arnuld | last post by:
i have slightly modified the programme from section 1.5.1 which takes the input frm keyboard and then prints that to the terminal. it just does not run and i am unable to understand the error...
12
by: arnuld | last post by:
#include <stdio.h> int main() { printf("%d\n", getchar() != EOF); return 0; } when i run this programme, no matter what i give it as input, output
20
by: Senthil-Raja | last post by:
The getchar() function is expected to fetch the next character in the input stream and return it. But, when I wrote a program using this function, it looks like the reading of the input stream...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.