Thread: Relational and Logical Operators

  1. #1
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134

    Exclamation Relational and Logical Operators

    Hello all

    I have a small problem with these operators as they dont seem to give the answer to what i would expect them to,
    any help with a decent explination would be appreciated
    Thanx in advance.

    Heres my code#include <stdio.h>

    int main()
    {
    int i;
    int j;

    printf("Enter first number"); /*number entered will be 5 */
    scanf("%d", &i);
    printf("Enter first number"); /*number entered will be 0 */
    scanf("%d", &j);

    printf("i < j %d\n", i<j); /*expected answer as 5 is greater than 0 "TRUE therefor 1" */

    printf("i <= j %d\n", i<=j); /*expected answer as 5 is greater than 0 "TRUE therefor 1" */

    printf("i == j %d\n", i==j); /*expected answer as 5 is not equal to 0 "FALSE therefore 0 */

    printf("i > j %d\n", i>j); /*expected answer as 5 is not less than 0 "TRUE 0" */

    printf("i >= j %d\n", i>=j); /*expected answer as 5 is not less than or equal to 0 "TRUE 0" */

    When the program is run the following output is displayed and I dont understand why ??

    Enter no 5
    Enter no 0

    i > j 0
    i <= j 0
    i == j 0
    i > j 1
    i >= j 1

    press any key to continue ...

    Think my logic here is up the pole but any help would be great

    Thanks
    GSLR

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Ok - at the start of the program you enter:

    i = 5
    j = 0

    therefore:
    i < j is FALSE as 5 is not LESS than 0 - Therefore FALSE or 0
    Code:
    printf("i < j %d\n", i<j); /*expected answer as 5 is greater than 0 "TRUE therefor 1" */
    < - Less than operator
    <= - Less than or equal to operator
    == - Equal to operator
    > - Greater than operator
    >= - Greater than or equal to operator
    != - Does not equal operator

    The programs output is correct. (i > j 0 with this exception which I think was a typo on your behalf )
    Last edited by foniks munkee; 03-01-2002 at 07:52 PM.

  3. #3
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134

    thanx

    thanx

    I realised me error after i posted

    Looks like i need a course in maths again !!!

Popular pages Recent additions subscribe to a feed