Thread: easy logic question

  1. #1
    Registered User
    Join Date
    Dec 2014

    easy logic question


    I'm a freshman EE/CS major in college, and just learned the basics of python in my 1000 level programming course this semester.

    I've been trying to begin to learn C, as I'll be needing it for my EE embedded control classes in the future, and have been going through the first couple tutorials on this site, which have been very helpful.

    I've run into an issue though regarding 'not equal to' in while/if statements.

    I have the following program:

    /*asks for input. If the input is equal to 5, end.*/
    #include <stdio.h>
    int main()
      int x;
      x = 6;
      while (x != 5){
        x = getchar();
    If I run it in the command shell, and I enter 5, the program continues and asks for another input.

    If I use a for loop, '!=' works. If I use it in an if statement, it does not work.

    Anybody see the issue? Can I not use '!=' in the parameters of my while loops/if statements in C?

    I'm sure the answer is staring me in the face. If you've taken the time to look at my program and try to find the issue, thank you.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    New York, NY
    Type in a 5 as a response, and getchar() returns the character '5' (ASCII 53 decimal) and you are testing x against the integer 5 in the while statement! 5 != 53!

  3. #3
    Registered User
    Join Date
    Sep 2014
    Quote Originally Posted by millsj2 View Post
      while (x != 5){
        x = getchar();
    The problem is 'x != 5', because you read new values with 'getchar' on 'x', 'x' will have the ascii-value of the character you entered.
    If you mean the key '5', then use:
      while (x != '5'){
        x = getchar();
    If the value stay alone, then you mean the decimal value.
    If the value stay in single quotes, then you mean the character, exactly the internal value (mostly ascii-value) that was used for the character '5'.


    The main function returns a integer value, so you should put a 'return 0;' befor the last bracket.
    Last edited by WoodSTokk; 12-14-2014 at 06:37 PM.
    Other have classes, we are class

  4. #4
    Registered User
    Join Date
    Dec 2014
    having 5 in quotes did the trick!

    Thanks for the answers, I did not know you needed quotes if you mean the key value.

    That also explains why when I type

    printf("%i", '5')

    C returns 53. I was confused about that.

    Thanks again,

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Python allows you to make that distinction too. While there's no char type like there is in C, python has the functions chr() and ord():
    # Python
    s = 'a'
    first = s[0]
    assert firstChar == s # In python a "char" is just a string
    assert ord(firstChar) == 97
    assert chr(97) == firstChar
    /* C */
    /* In C, strings end with an invisible null character.
       Hence the size 2 rather than 1 */
    const char s[2] = "a"; 
    /* The first element of s is a char, which is not a string.
       'a', not "a" */
    char firstChar = s[0];
    /* This comparison wouldn't mean what you think. */
    /* assert(firstChar == s); */
    /* 'a' and 97 mean the same thing in ASCII */
    assert(firstChar == 97);
    assert(firstChar == 'a');
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logic question
    By Roy01 in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2006, 09:02 AM
  2. Help logic question! C =P
    By Eladmir in forum C Programming
    Replies: 6
    Last Post: 03-04-2005, 04:27 PM
  3. logic question...
    By rox in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2004, 10:03 AM
  4. Logic Question
    By RoD in forum A Brief History of
    Replies: 31
    Last Post: 10-24-2002, 01:41 PM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM

Tags for this Thread