Thread: Why doesn't this work? (simple)

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403

    Why doesn't this work? (simple)

    I would have thought the following code would print out integers if they were inputted......

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int c;
    
    while ((c = fgetc(stdin)) != 'Q')
      printf("%d\n", c);
    
    return 0;
    
    }
    I've read that when using getchar() or fgetc() you can use " c - '0' " in place of c in the printf statement to rectifiy the problem, (what does it do?) if i do so, i get the integer inputted printed to the screen but i also get a another negative number printed underneath it.

    Can someone tell me whats going on?

    Many thanks.

  2. #2
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16
    variable 'c' should be a char and the format specifier will need to be a %c:
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    char c;
    
    while ((c = fgetc(stdin)) != 'Q')
      printf("%c\n", c);
    
    return 0;
    
    }
    Hope that works, MrB.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I would have thought the following code would print out
    > integers if they were inputted......

    > printf("%d\n", c);

    All you're doing here is displaying the decimal value of the character that has been entered. If you're after numbers then yes, c-'0' will work.

    For example, let's say that '0' has the decimal (ascii) value of 35. If I enter a '1', then it's value is 36, and '2' is 37 and so on.
    That being the case:

    37 - 35 = 2 (ie: '2' - '0' = 2)

    Not a hard concept.

    > variable 'c' should be a char and the format specifier will need
    > to be a %c:

    No. Because they want to display the numerical value. As such, %d is what they want.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    Thanks quzah, that makes sense.

    Though i'm still puzzled by the output of this code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int c;
    
    while ((c = fgetc(stdin)) != 'Q')
      printf("%d\n", c - '0');
    
    return 0;
    
    }
    I would have thought when running it, if i entered say 5, it would just print 5 right back out........ instead it prints 5 then underneath the 5 it prints -38.......

    where is the -38 coming from?
    Last edited by Clyde; 04-02-2002 at 05:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  2. Simple Winsock Wrapper
    By neandrake in forum Networking/Device Communication
    Replies: 12
    Last Post: 03-22-2004, 10:55 PM
  3. Simple class doesn't work
    By Cris987 in forum C++ Programming
    Replies: 7
    Last Post: 01-08-2004, 11:18 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM