getchar Query

This is a discussion on getchar Query within the C Programming forums, part of the General Programming Boards category; Hi All, am new to C programming. I am following this online tutorial, http://publications.gbdirect.co.uk/c..._programs.html and tried this example, Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Sydney, Australia
    Posts
    6

    getchar Query

    Hi All,

    am new to C programming. I am following this online tutorial,

    http://publications.gbdirect.co.uk/c..._programs.html

    and tried this example,

    Code:
    #include <stdio>
    #include <stdlib.h>
    main(){
            int ch;
    
            ch = getchar();
            while(ch != 'a'){
                    if(ch != '\n')
                            printf("ch was %c, value %d\n", ch, ch);
                    ch = getchar();
            }
            exit(EXIT_SUCCESS);
    }
    and got this output,

    :~/scripts$ ./char
    k
    char is k, value 107
    char is k, value 107

    I do not understand why it prints the result twice, and what is the reason for the 2nd ch = getchar() after the printf statement. Could anyone comment on this example code? Cheers.

    coolboarerguy

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to include <stdio.h>, not <stdio>, and you need an "int" in front of main.

    The second getchar just keeps getting the next character from the input stream. (Since it's inside a loop, it may happen a bunch of times.)

    The only way to get two statements like that is to type k twice. Even if a control character other than \n got in there, like \r or something, it would still print that instead of k.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Sydney, Australia
    Posts
    6
    Hi All,

    sorry, that was copy/pasted from the site, whilst my code had <stdio.h> in it, as I did it from my own memory of the example code. I did pick up on the need for int instead of char. I still don't understand why it prints it twice. Cheers.

    coolboarderguy

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
            int ch;
            ch = getchar();
            while(ch != 'a'){
                    if(ch != '\n')
                            printf("char is %c, value %d\n", ch, ch);
                            getchar();
                            }
    exit(EXIT_SUCCESS);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because you lost the "ch =" in front of the getchar(); the second time.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Sydney, Australia
    Posts
    6
    DOH!!! Thanx.

    coolboarderguy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 01:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-05-2004, 12:06 AM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21