LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-20-2009, 06:57 PM   #1
paulclark
LQ Newbie
 
Registered: Dec 2006
Posts: 18

Rep: Reputation: 0
Newbie C Question on character constants.


I am puzzled by the code below. I thought this would exit the 'While' loop as soon as x was pressed on the keyboard. It does not, no matter how many times x is pressed. Yet x+<CR> does. Am I comparing the numeric value in the character set here as I think I should be or something else as I seem to be?

Paul Clark


Code:
int c;
	c = getchar();
	while (c != 'x') {
		putchar(c);
		c = getchar();
        }

Last edited by paulclark; 10-20-2009 at 06:59 PM.
 
Old 10-20-2009, 07:21 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by paulclark View Post
I am puzzled by the code below. I thought this would exit the 'While' loop as soon as x was pressed on the keyboard. It does not, no matter how many times x is pressed. Yet x+<CR> does. Am I comparing the numeric value in the character set here as I think I should be or something else as I seem to be?

Paul Clark


Code:
int c;
	c = getchar();
	while (c != 'x') {
		putchar(c);
		c = getchar();
        }
getchar() is line buffered
that means you have to press return before the key is accepted
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed newbie question - strip leading character only if 0 Frayed Programming 6 05-22-2008 10:30 AM
Newbie character driver questions kamputty Linux - Kernel 2 04-06-2007 01:39 AM
c: define constants kpachopoulos Programming 5 08-19-2006 03:31 PM
LXer: Variables Won't Constants Aren't: LXer Syndicated Linux News 0 07-26-2006 04:33 PM
CSS - Constants? dushkinup Programming 1 05-02-2004 01:08 PM


All times are GMT -5. The time now is 10:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration