does the logical expression return 1 ( if it is true ) absolutely?

Discussion in 'C Programming' started by moosdau, Dec 28, 2005.

  1. moosdau

    moosdau Guest

    it's likely that the compiler could only promise returning a non-zero
    value in my impression,
    but it did return 1 every time,
    like the code below:
    int a=3,b=8;
    printf("%d\n",a<b);

    will it return 1 absolutely in any case?
     
    moosdau, Dec 28, 2005
    #1
    1. Advertising

  2. On 27 Dec 2005 20:21:06 -0800, "moosdau" <> wrote:

    >it's likely that the compiler could only promise returning a non-zero
    >value in my impression,
    >but it did return 1 every time,
    >like the code below:
    >int a=3,b=8;
    >printf("%d\n",a<b);
    >
    >will it return 1 absolutely in any case?


    Yes. To quote from K&R: "By definition, the numeric value of a
    relation or logical expression is 1 if the relation is true and 0 if
    the relation is false." I really doubt the new standard would change
    this since a lot of code depends on it.


    <<Remove the del for email>>
     
    Barry Schwarz, Dec 28, 2005
    #2
    1. Advertising

  3. moosdau

    moosdau Guest

    thanks very much!
    I didn't read K&R 's book when I learn c.
    the book I read only refer that the value is non zero,didn't mention it
    must be 1.
    now I know.:)
     
    moosdau, Dec 28, 2005
    #3
  4. moosdau

    Anand Guest

    Barry Schwarz wrote:
    > On 27 Dec 2005 20:21:06 -0800, "moosdau" <> wrote:
    >
    >
    >>it's likely that the compiler could only promise returning a non-zero
    >>value in my impression,
    >>but it did return 1 every time,
    >>like the code below:
    >>int a=3,b=8;
    >>printf("%d\n",a<b);
    >>
    >>will it return 1 absolutely in any case?

    >
    >
    > Yes. To quote from K&R: "By definition, the numeric value of a
    > relation or logical expression is 1 if the relation is true and 0 if
    > the relation is false." I really doubt the new standard would change
    > this since a lot of code depends on it.
    >

    You are right, there's no change in this aspect, and C99 also defines it
    as 1 and 0.
    To quote from std:
    +----C99:6.5.8 Relational operators-----
    |6. Each of the operators < (less than), > (greater than), <=
    | (less than or equal to), and >= (greater than or equal to)
    | shall yield 1 if the specified relation is true and 0 if
    | it is false.
    | The result has type int.
    +---------------------------------------

    and 6.5.9 (Equality Operators) goes on to define the == and != as above.
    --
    (Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
    (clc FAQ) http://c-faq.com
     
    Anand, Dec 28, 2005
    #4
  5. moosdau

    John Bode Guest

    moosdau wrote:
    > it's likely that the compiler could only promise returning a non-zero
    > value in my impression,
    > but it did return 1 every time,
    > like the code below:
    > int a=3,b=8;
    > printf("%d\n",a<b);
    >
    > will it return 1 absolutely in any case?


    Yes. The result of a relational (<, <=, >, >=) or equality (==, !=)
    expression is always either 1 or 0, and is always of type int.

    It is also true that any non-zero integral expression in a Boolean
    context will evaluate to true, so given an int variable i=123,

    if (i != 0) {/* do stuff */}

    and

    if (i) {/* do stuff */}

    are equivalent.
     
    John Bode, Dec 28, 2005
    #5
  6. moosdau

    tmp123 Guest

    It is a usual confusion: one thing is that the "if" statement goes to
    the "then" part for any value different of 0 and another one is what is
    returned by C operators.
     
    tmp123, Dec 28, 2005
    #6
  7. moosdau

    Thad Smith Guest

    moosdau wrote:
    > thanks very much!
    > I didn't read K&R 's book when I learn c.
    > the book I read only refer that the value is non zero,didn't mention it
    > must be 1.
    > now I know.:)


    What confuses some people is that while equality and relational
    operators evaluate to 0 and 1, the operands of if(), while(), ?:, ||,
    &&, !, and middle operand of for(;;) are compared with 0, so any
    non-zero value will cause the same result of the test.


    --
    Thad
     
    Thad Smith, Dec 29, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    666
    Chris Theis
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    827
    Keith Thompson
    Mar 6, 2005
  3. Lars Uffmann

    Definition of logical true in C++?

    Lars Uffmann, Jan 21, 2008, in forum: C++
    Replies:
    19
    Views:
    898
    Lars Uffmann
    Jan 23, 2008
  4. bdb112
    Replies:
    45
    Views:
    1,346
    jazbees
    Apr 29, 2009
  5. w i l l
    Replies:
    4
    Views:
    240
    Dan Brussee
    Jul 4, 2003
Loading...

Share This Page