LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Newbie C Question on character constants. (https://www.linuxquestions.org/questions/programming-9/newbie-c-question-on-character-constants-763312/)

paulclark 10-20-2009 06:57 PM

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();
        }


smeezekitty 10-20-2009 07:21 PM

Quote:

Originally Posted by paulclark (Post 3726580)
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


All times are GMT -5. The time now is 04:27 PM.