getchar()

This is a discussion on getchar() within the C Programming forums, part of the General Programming Boards category; I have some code Code: int x; printf("1. Quit\n"); printf("2. Add number\n"); x=getchar(); if(x=='2'){ At first the 2 wasn't in ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    getchar()

    I have some code
    Code:
            int x;
            printf("1.  Quit\n");
            printf("2.  Add number\n");
            x=getchar();
            if(x=='2'){
    At first the 2 wasn't in character constant notation, but the if statement never worked. I printed %d of x and it came out 50. Why do I have to do this? getchar always returns an int, and I have never had this problem before?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by linuxdude
    I have some code
    Code:
            int x;
            printf("1.  Quit\n");
            printf("2.  Add number\n");
            x=getchar();
            if(x=='2'){
    At first the 2 wasn't in character constant notation, but the if statement never worked. I printed %d of x and it came out 50. Why do I have to do this? getchar always returns an int, and I have never had this problem before?
    What do you expect to get from the following:

    Code:
    #include <stdio.h>
    int main()
    {
      int x;
      
      printf("1.  Quit\n");
      printf("2.  Add number\n");
      x=getchar();
      printf("x = %c (%d decimal), (0x%02x hex)\n", x, x, x);
      if(x=='2'){ 
        printf("It passed the =='2' test.\n");
      }
      else{
        printf("It didn't pass the =='2' test.\n");
      }
    
     if (x == 50) {
        printf("It passed the ==50 test.\n");
      }
      else {
        printf("It didn't pass the ==50 test.\n");
      }
    
      return 0;
    }

    What do you get?

    What's the problem?

    Regards,

    Dave
    Last edited by Dave Evans; 09-05-2004 at 09:45 PM.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I see what it passed, but I dont' know why it doesn't pass the 2. getchar() returns an int I have an int, there shouldn't be a need for a conversion right?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by linuxdude
    I see what it passed, but I dont' know why it doesn't pass the 2. getchar() returns an int I have an int, there shouldn't be a need for a conversion right?
    The int returned from getchar() when you press the '2' key has a value of 50 decimal. By assigning the integer value 50 to the char variable x, x now has the value 50 decimal.

    When you tell printf to print x as a %c, it converts to the ascii printable character '2'. When you tell printf to print it as a %d, it prints the decimal value. When you tell printf() to print it as a %x, it prints the hex value.

    When you compare x with '2', it passes, since '2' has the decimal value 50 and so does x.

    When you compare x with 50, it passes, since x has the decimal value of 50

    ("Bits is bits.")

    Dave
    Last edited by Dave Evans; 09-05-2004 at 10:02 PM.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    hmm I though all characters from the keyboard were ints.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by linuxdude
    hmm I though all characters from the keyboard were ints.
    getchar() returns an int.

    If you press the key '2', getchar() returns an int with value 50 decimal.

    In the C language, the notation '2' indicates a "character constant" whose value is the ascii representation of '2', which happens to be 0x32, or decimal 50.

    If you set a char equal to an int, the conversion is made automatically in C by chopping off the upper bits. Since the upper bits of 50 decimal are all zero, the conversion doesn't seem to change anything.

    Forgetting getchar() for the moment, try this:
    Code:
    #include <stdio.h>
    int main()
    {
      int xx;
      char cc;
    
      xx = 0x1234;
      cc = xx;
      printf("xx = %d (decimal), cc = %d (decimal)\n", xx, cc);
      printf("xx = %x(hex), cc = %x(hex)\n", xx, cc);
    
      return 0;
    }
    Dave

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by linuxdude
    I have some code
    Code:
            int x;
            printf("1.  Quit\n");
            printf("2.  Add number\n");
            x=getchar();
            if(x=='2'){
    At first the 2 wasn't in character constant notation, but the if statement never worked. I printed %d of x and it came out 50. Why do I have to do this? getchar always returns an int, and I have never had this problem before?
    Are you confusing it with this?
    Code:
    scanf("%d", &x);
    Quote Originally Posted by linuxdude
    hmm I though all characters from the keyboard were ints.
    Like 'a', 'b', 'c', '1', '2', '3' which all have integer values?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by linuxdude
    hmm I though all characters from the keyboard were ints.
    Characters from the keyboard are whatever the function that gets them gives you.

    When you press the '2' key, getchar() returns an int that has the value of the ascii representation of '2'. Some other function could give you something else.

    Dave
    Last edited by Dave Evans; 09-05-2004 at 10:19 PM.

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