473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

getchar() behaviour on gcc 4.1

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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Apr 5 '06 #1
20 2614
ram
your input 'y' is equal to the condition i.e the condition becomes true
,so it will again ask u the input.please note the only when the
condition fails the loop will terminate.

Apr 5 '06 #2
blufox wrote:
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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'
Apr 5 '06 #3
- Show quoted text -
blufox wrote:
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');
return 0;
} Output ::
[rautela@blufox c]$ ./a.out
still yes... enter choice :: y
still yes... [rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?


Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'

got it. Will try to fix it.
Thank you

Apr 5 '06 #4
blufox wrote:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
fflush(stdout);
/* Otherwise there is no guarantee the output will be displayed before
waiting for input. Never heard of line buffering? */
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?


Yes. How many keys did you press at the first prompt? I'll bet it was
more than one...

Generally it is better to use fgets (not gets, NEVER gets) to read a
complete line and then process it. It is far simpler to get it right
that way.
--
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 5 '06 #5
blufox wrote:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");
fflush(stdout); /* so that you are guaranteed
to see the output before
getchar() executes */
ch = getchar();
}while(ch == 'y');
/* Try this */
printf("do loop exited with ch == '%c' (value == %d).\n", ch, ch);
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?


Did you just press 'y' ... or was it 'y'<ENTER>?

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Apr 5 '06 #6
It's right.
You can print out the value of ch after calling getchar.
The code like this:
int main(int argc, char* argv[])
{
int ch;
do
{
printf("still yes...\n");
printf("\nenter choice :: ");
ch = getchar();
printf("ch='%c'\n",ch);
}while(ch == 'y');
return 0;

}

and the output will be:
still yes...

enter choice :: y
ch='y'
still yes...

enter choice :: ch='
'

"Nils O. Selåsdal" <NO*@Utel.no> ??????:44******@news.broadpark.no...
blufox wrote:
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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'

Apr 5 '06 #7

blufox wrote:
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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?


Describe to your self in English(or your native language) what it is
you are trying to accomplish.

Then describe to your self in English(or your native language) what it
is the C code is in fact accomplishing.

You shall see that the C logic prevails.

Apr 5 '06 #8

Nils O. Selåsdal wrote:
blufox wrote:
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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'


You are a detriment to this group.

Apr 5 '06 #9

Flash Gordon wrote:
blufox wrote:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");


fflush(stdout);
/* Otherwise there is no guarantee the output will be displayed before
waiting for input. Never heard of line buffering? */
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?


Yes. How many keys did you press at the first prompt? I'll bet it was
more than one...

Generally it is better to use fgets (not gets, NEVER gets) to read a
complete line and then process it. It is far simpler to get it right
that way.
--
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


You are a detriment to this group.

Apr 5 '06 #10
"Ivanna Pee" <fr***@Infectedmail.com> wrote:

[ Nothing but: ]
You are a detriment to this group.


Kenny: piss off again.

Richard
Apr 5 '06 #11
On Wed, 05 Apr 2006 03:54:08 -0700, blufox wrote:
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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?

if you add "printf( "--- %d --- \n",ch);" just before the
end of the loop you'll see whats happening:
When you typed 'y' you also hit enter. The second call to getchar()
returns 10, ie '\n' and the loop exits.
Duncan
Apr 5 '06 #12
Ivanna Pee wrote:
Nils O. Selåsdal wrote:
blufox wrote:
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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here? Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'


In another post to this thread you said;
"Describe to your self in English(or your native language) what it is
you are trying to accomplish."
Perhaps you should take your own advice, and try to explain the reasoning
behind: You are a detriment to this group.

Apr 5 '06 #13
Nils O. Selåsdal wrote:
Ivanna Pee wrote:
Nils O. Selåsdal wrote:
blufox wrote:
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');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?
Consider what happens when you inputted y
You hit y , then you likely hit the enter key. The first getchar
call reads the 'y'. The next might read the enter key-hit as a '\n'


In another post to this thread you said;
"Describe to your self in English(or your native language) what it is
you are trying to accomplish."
Perhaps you should take your own advice, and try to explain the reasoning
behind:
You are a detriment to this group.


Based on his other posts, he appears to be a troll who takes objection
to people providing useful information to posters in the form of direct
answers preferring instead that people provide "hints" that still
require the questioner to figure out the problem themselves. It would
probably be best to ignore his posts.

Robert Gamble

Apr 5 '06 #14
ram wrote:

your input 'y' is equal to the condition i.e the condition becomes true
,so it will again ask u the input.please note the only when the
condition fails the loop will terminate.


Who is 'your', who is 'u'. What loop? Without proper context your
article is meaningless. Google is NOT usenet, it is only a poor
excuse for an interface to usenet. There is no reason to assume
that your reader has ever seen, or will ever see, any other
messages. Thus an article has to stand by itself. You do this by
including context, snipped to material germane to your reply. For
means of doing this, even on google, see my sig. below. Please
also read the reference URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 5 '06 #15
Flash Gordon wrote:
blufox wrote:
This is a simple code snippet i used ...
#include <stdio.h>
int main(void)
{
int ch;
do {
printf("still yes...\n");
printf("\nenter choice :: ");


fflush(stdout);
/* Otherwise there is no guarantee the output will be displayed
before waiting for input. Never heard of line buffering? */
ch = getchar();
}while(ch == 'y');
return 0;
}

Output ::
[rautela@blufox c]$ ./a.out
still yes...

enter choice :: y
still yes...

[rautela@blufox c]$
Why didn't it waits for my input the next time?
I am compiling this on a Fedora Core 5 using gcc 4.1.
Am i missing something here?


Yes. How many keys did you press at the first prompt? I'll bet it was
more than one...

Generally it is better to use fgets (not gets, NEVER gets) to read a
complete line and then process it. It is far simpler to get it right
that way.


Using fgets or the equivalent requires a buffer, and checking for
line completion etc. A more positive way is to insist on an
'enter' as a response to the wait, and handle it with a call to:

flushln(stdin);

in place of the getchar() call. The flushln routine will be useful
in many places, and should be:

int flushln(FILE *f) {
int ch;
while ((EOF != (ch = getc(f))) && (ch != '\n')) continue;
return ch;
}

No buffer, or other storage is required. Now, if you want a
specific single non-blank character response, you can use:

int getreply(FILE *f) {
int ch;
while (' ' == (ch = getc(f))) continue;
if ('\n' != ch) flushln(f);
return ch;
}

Note that all these things can return EOF. A few such routines in
your toolbox go a long ways to keeping interactive input clean.
The routines are more useful when the input file is parametized.
Note that getc(stdin) is equivalent to getchar().

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 5 '06 #16
Ivanna Pee wrote:

You are a detriment to this group.


*plonk*

Brian
Apr 5 '06 #17
Robert Gamble opined:
Nils O. Selåsdal wrote:
Ivanna Pee wrote:
> Nils O. Selåsdal wrote:
>> blufox wrote:
>>> 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');
>>> return 0;
>>> }
>>>
>>> Output ::
>>> [rautela@blufox c]$ ./a.out
>>> still yes...
>>>
>>> enter choice :: y
>>> still yes...
>>>
>>> [rautela@blufox c]$
>>> Why didn't it waits for my input the next time?
>>> I am compiling this on a Fedora Core 5 using gcc 4.1.
>>> Am i missing something here?
>> Consider what happens when you inputted y
>> You hit y , then you likely hit the enter key. The first getchar
>> call reads the 'y'. The next might read the enter key-hit as a
>> '\n'


In another post to this thread you said;
"Describe to your self in English(or your native language) what it
is you are trying to accomplish."
Perhaps you should take your own advice, and try to explain the
reasoning behind:
> You are a detriment to this group.


Based on his other posts, he appears to be a troll who takes
objection to people providing useful information to posters in the
form of direct answers preferring instead that people provide "hints"
that still
require the questioner to figure out the problem themselves. It
would probably be best to ignore his posts.


In my native language (after spelling is abstracted) he'd be a she...
It'd be the first she-troll I encountered.

--
Dijkstra probably hates me
(Linus Torvalds, in kernel/sched.c)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 5 '06 #18
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Robert Gamble opined:

[...]
Based on his other posts, he appears to be a troll who takes
objection to people providing useful information to posters in the
form of direct answers preferring instead that people provide "hints"
that still
require the questioner to figure out the problem themselves. It
would probably be best to ignore his posts.


In my native language (after spelling is abstracted) he'd be a she...
It'd be the first she-troll I encountered.


Not that it matters, but "Ivanna Pee" is almost certainly not this
person's real name; the person behind the alias could as easily be
male as female. ("Ivanna" --> "I wanna" --> "I want to"; "pee" is
slang for "urinate".)

--
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 5 '06 #19
Keith Thompson opined:
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Robert Gamble opined:

[...]
Based on his other posts, he appears to be a troll who takes
objection to people providing useful information to posters in the
form of direct answers preferring instead that people provide
"hints" that still
require the questioner to figure out the problem themselves. It
would probably be best to ignore his posts.


In my native language (after spelling is abstracted) he'd be a
she... It'd be the first she-troll I encountered.


Not that it matters, but "Ivanna Pee" is almost certainly not this
person's real name; the person behind the alias could as easily be
male as female. ("Ivanna" --> "I wanna" --> "I want to"; "pee" is
slang for "urinate".)


Ah, now it becomes clear. I should have got it earlier...

PS
I made sure limericks stay out of my sig. They seem to have got around
my request not to include offensive fortunes.

--
Linux is obsolete
(Andrew Tanenbaum)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 5 '06 #20
Ivanna Pee wrote:
Flash Gordon wrote:

.... snip ...

Yes. How many keys did you press at the first prompt? I'll bet it
was more than one...

Generally it is better to use fgets (not gets, NEVER gets) to read
a complete line and then process it. It is far simpler to get it
right that way.


You are a detriment to this group.


You are gone. PLONK

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 6 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may...
1
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if...
5
by: Jonathan | last post by:
Hi-- I have the following code: #include <stdio.h> char a,b; int main()
10
by: john | last post by:
What does the standard say about getchar()? Do you have to press return to "send" the char to the program, or is it implementation defined? I read in a book that on some systems the function...
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...
22
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.