# 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. ## 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. 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. 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));`

4. Code:
printf("Is B Greater Than C? %s\n", B > C ? "true" : "false");

5. 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. 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. 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. <> 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");

9. 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. 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 ...