Relational Operators

This is a discussion on Relational Operators within the C Programming forums, part of the General Programming Boards category; I'm taking a programming course, and have never wrote code before so this might seem like a dumb question... I ...

  1. #1
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16

    Relational Operators

    I'm taking a programming course, and have never wrote code before so this might seem like a dumb question...

    I was given some values for some variables ( B=5 C=10 etc...) and was told to use the different Relational Operators with the sample equations given.

    So far I have:

    int B=5, C=10, D=20, E=64, F=2, A;

    printf("Is B Greater Than C? %d\n", B>C);
    I can't get it to output true or false ... I keep getting 0 or 1 and I haven't found a reference anywhere to tell me how to get it to say false or true when it is compiled.

    Thanks

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    That's note the way to do it. When you perform a comparison, the operator does not literally return "true", it just returns a boolean value understood to mean true. What you should do is use some conditionals.

    Code:
    if(B>C) printf("B is greater than C");
    if(B<C) printf("C is greater than B");

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    or:

    Code:
    printf("Is B Greater Than C? %s\n", B > C ? "true" : "false");
    or as a function:

    Code:
    const char * boolStr(int boolean) {
     if(boolean) {
      return "true";
      } else {
      return "false";
      }
     }
    Code:
    printf("Is B Greater Than C? %s\n", boolStr(B > C));
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    printf("Is B Greater Than C? %s\n", B > C ? "true" : "false");
    Very nice! I had totally forgotten about that operator!

  5. #5
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    Thanks for the help, saved me from a major headache.

    I went with

    printf("Is B Greater Than C? %s\n", B > C ? "true" : "false");
    and it worked out great.

    I have another problem now which stems from this though ...

    printf("Is D Greater than or less than 20? %s\n", (D<>20) ? "True" : "False");
    I get a parse error when i go to compile.

    25 c:\mydocu~1\elizab~1\progra~1\lab22~1.c
    parse error before `>'

    25 c:\mydocu~1\elizab~1\progra~1\lab22~1.c
    parse error before A-`n\n"(a~122~1In fion n':
    and cannot figure out what is wrong.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    D<>20
    There's no such operator... If you mean to see if it's either of the two, the not-equal operator is !=, but you might have just hit an extra key by mistake.

  7. #7
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    I didnt think it was an operator ... but its there on the lab sheet as an equation...

    "D<>20"

    I'll just go with != then and hope it works out teh way he wants it.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    <> isn't an operator. I'm not sure what you're trying to do there but you can string together ternary operators like this:

    printf("D is %s 20.\n", D > 20 ? "Greater Than" : D < 20 : "Less Than" : "Equal");
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    but its there on the lab sheet as an equation...
    I feel sorry for you, if you're having to learn C from a guy that actually thought that that was an operator, but at the same time it's pretty funny

  10. #10
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    rofl I can see where its funny for you, but not so funny for me.

    He gave us a lab in which we have to make 2 programs ... and he has yet to teach us any c? So this has been real interesting ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 11-30-2008, 10:30 PM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. problem with relational operators
    By manoncampus in forum C++ Programming
    Replies: 10
    Last Post: 12-13-2005, 06:15 PM
  4. problem with relational operators
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 09:45 AM
  5. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 03:33 PM

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