return a==b;

Discussion in 'C Programming' started by google.bo@olive-it.ch, May 15, 2008.

  1. Guest

    I don't remember the exact syntax, however I've heard that
    return a==b;
    is valid c (like in gcc does not complain)
    my questions:
    1) is this real C standard?
    2) as there is no true nor false in C (just 0 and not 0) what does
    a==b have for a value, if any?
    thanx
    olivier
     
    , May 15, 2008
    #1
    1. Advertising

  2. writes:
    > I don't remember the exact syntax, however I've heard that
    > return a==b;
    > is valid c (like in gcc does not complain)


    Yes, it's valid C (assuming appropriate declarations of a and b), and
    yes, I expect that gcc wouldn't complain about it. (There are a number
    of things, gcc-specific extensions, that gcc doesn't complain about
    by default, even though they aren't valid C (though just what "valid C"
    means is subject to some debate).)

    > my questions:
    > 1) is this real C standard?
    > 2) as there is no true nor false in C (just 0 and not 0) what does
    > a==b have for a value, if any?


    As your C reference should tell you, the "==" operator yields a result
    of type int, with the value 1 if the operands are equal and 0 if they
    are not.

    In general, built-in operators that yield "boolean" results yield 0
    for false, 1 for true. Library functions such as isupper() return 0
    for false, some unspecified non-zero value for true. Contexts that
    require a condition (such as an if or while) treat 0 as false,
    anything else as true.

    (C99 adds a built-in boolean type, but all the existing rules stay the
    same.)

    The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 9 covers
    "Boolean Expressions and Variables".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 15, 2008
    #2
    1. Advertising

  3. On 15 May 2008 at 22:30, Richard Heathfield wrote:
    > Pietro Cerutti said:
    >>> 2) as there is no true nor false in C (just 0 and not 0) what does
    >>> a==b have for a value, if any?

    >>
    >> 0 if a and be differ, some implementation-defined value other than 0 if
    >> they're equal (usually 1).

    >
    > That's not how I read the Standard.


    Or just apply common sense. At the machine code level, there will be a
    comparison instruction followed by one that sets a register to the
    current value (0 or 1) of the zero flag.
     
    Antoninus Twink, May 15, 2008
    #3
  4. Walter Banks Guest

    Richard Heathfield wrote:

    >
    > That's not how I read the Standard. Could you please provide chapter and
    > verse? (Mine are 3.3.8: "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." and
    > 3.3.9: "The == (equal to) and the != (not equal to) operators are
    > analogous to the relational operators except for their lower precedence".
    > This suggests strongly that == must yield either 0 or 1. If it's actually
    > implementation-defined, the Standard will document this fact somewhere,
    > but I can find no such reference.)


    You are correct. It is 0 or 1 not implementation defined.

    if (a) ....

    tests 0 or non zero

    a = b < c; returns 0 or 1

    w..
     
    Walter Banks, May 15, 2008
    #4
  5. [Adding, not correcting...]

    Richard Heathfield wrote:
    > > 2) as there is no true nor false in C (just 0 and not 0) what
    > > does a==b have for a value, if any?

    >
    > == yields either 0 (a and b have different values) or 1 (a and
    > b have the same value), guaranteed.


    With technical exceptions of trap representations and signaling
    floating point numbers.

    > An equality expression is still an expression,


    Moreover, relational and equality operators are just operators.

    > so there is no problem using it in a return statement.


    Unless it's used in a void function.

    --
    Peter
     
    Peter Nilsson, May 15, 2008
    #5
  6. Guest

    On May 16, 12:30 am, Richard Heathfield <> wrote:

    > That's not how I read the Standard. Could you please provide chapter and
    > verse? (Mine are 3.3.8: "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." and
    > 3.3.9: "The == (equal to) and the != (not equal to) operators are
    > analogous to the relational operators except for their lower precedence".
    > This suggests strongly that == must yield either 0 or 1. If it's actually
    > implementation-defined, the Standard will document this fact somewhere,
    > but I can find no such reference.)
    >

    Is there a pointer to "The Standard" you refer? I have just the K&R
    book, and yes, I just found there the reference, hidden in chapter
    2.6:
    "By definition, the numeric value of a relational or logical
    expression is 1 if the relation is true, and ' if the relation is
    false."
    That's my reference:)

    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999


    olivier
     
    , May 15, 2008
    #6
  7. Peter Nilsson <> writes:
    > [Adding, not correcting...]
    >
    > Richard Heathfield wrote:
    >> > 2) as there is no true nor false in C (just 0 and not 0) what
    >> > does a==b have for a value, if any?

    >>
    >> == yields either 0 (a and b have different values) or 1 (a and
    >> b have the same value), guaranteed.

    >
    > With technical exceptions of trap representations and signaling
    > floating point numbers.
    >
    >> An equality expression is still an expression,

    >
    > Moreover, relational and equality operators are just operators.
    >
    >> so there is no problem using it in a return statement.

    >
    > Unless it's used in a void function.


    Or in a function that returns a non-arithmetic type (a pointer,
    struct, or union).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 15, 2008
    #7
  8. wrote:
    > I don't remember the exact syntax, however I've heard that
    > return a==b;
    > is valid c (like in gcc does not complain)
    > my questions:
    > 1) is this real C standard?


    It's trivial standard. 'a==b' is an expression with a value, and you
    return that value.

    > 2) as there is no true nor false in C (just 0 and not 0) what does
    > a==b have for a value, if any?


    That's not quite right. There are many operators and functions that
    yield a 'true' or 'false' result. Since 'true' and 'false' are
    philosophical concepts and not values, the results actually produces for
    predicate operations and functions are 1 and 0. Only in interpreting
    results as predicates but from from operations and functions which are
    not essentially predicates is it true that non-zero is equivalent to 1.
     
    Martin Ambuhl, May 16, 2008
    #8
  9. Keith Thompson wrote:
    > Peter Nilsson <> writes:
    > > Richard Heathfield wrote:
    > > > An equality expression is still an expression,
    > > > so there is no problem using it in a return statement.

    > >
    > > Unless it's used in a void function.

    >
    > Or in a function that returns a non-arithmetic type (a pointer,
    > struct, or union).


    In the case of a pointer, it may be used in a null pointer constant
    context, e.g. ...

    void *null(void) { return 0 == 1; }

    ....though that's not advisable. :)

    --
    Peter
     
    Peter Nilsson, May 16, 2008
    #9
  10. Bart Guest

    On May 16, 12:31 am, Martin Ambuhl <> wrote:
    > wrote:


    > > 2) as there is no true nor false in C (just 0 and not 0) what does
    > > a==b have for a value, if any?

    >
    > That's not quite right.  There are many operators and functions that
    > yield a 'true' or 'false' result.  Since 'true' and 'false' are
    > philosophical concepts and not values, the results actually produces for
    > predicate operations and functions are 1 and 0.  Only in interpreting
    > results as predicates but from from operations and functions which are
    > not essentially predicates is it true that non-zero is equivalent to 1.


    I think that paragraph should be taken out and shot :)

    --
    Bartc
     
    Bart, May 16, 2008
    #10
  11. wrote:
    > On May 16, 12:30 am, Richard Heathfield <> wrote:
    >
    >> That's not how I read the Standard. Could you please provide chapter
    >> and verse? (Mine are 3.3.8: "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." and
    >> 3.3.9: "The == (equal to) and the != (not equal to) operators are
    >> analogous to the relational operators except for their lower
    >> precedence". This suggests strongly that == must yield either 0 or
    >> 1. If it's actually implementation-defined, the Standard will
    >> document this fact somewhere, but I can find no such reference.)
    >>

    > Is there a pointer to "The Standard" you refer? I have just the K&R
    > book, and yes, I just found there the reference, hidden in chapter
    > 2.6:
    > "By definition, the numeric value of a relational or logical
    > expression is 1 if the relation is true, and ' if the relation is
    > false."
    > That's my reference:)

    You'll find C99 + TC 1, 2 and 3 here:
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

    Bye, Jojo
     
    Joachim Schmitz, May 16, 2008
    #11
  12. Guest

    On 16 Mai, 00:12, wrote:
    > I don't remember the exact syntax, however I've heard that
    > return a==b;
    > is valid c (like in gcc does not complain)
    > my questions:
    > 1) is this real C standard?
    > 2) as there is no true nor false in C (just 0 and not 0) what does
    > a==b have for a value, if any?
    > thanx
    > olivier


    As explained already in other mails a==b is okay and delivers
    0 or 1. So it is exactly defined. It can be used in every
    place where an integer (boolean) expression is allowed.

    Maybe you confused the behaviour of == (which is exactly
    defined) with the behaviour of strcmp and memcmp, which are
    not defined to return one of the values -1, 0 and 1.
    Instead strcmp and memcmp can return any integer value.

    Greetings Thomas Mertes

    Seed7 Homepage: http://seed7.sourceforge.net
    Seed7 - The extensible programming language: User defined statements
    and operators, abstract data types, templates without special
    syntax, OO with interfaces and multiple dispatch, statically typed,
    interpreted or compiled, portable, runs under linux/unix/windows.
     
    , May 16, 2008
    #12
  13. In article <>,
    Richard Heathfield <> wrote:
    ....
    > 10 108 629


    This is typical Heathfield. What Richard refers to as his
    (Heathfield's) excessively flowery language - with his usual sarcasm
    thrown in.

    All that was necessary was to say:

    "No, it has to be 1 (see 3.3.8)"

    For which, wc reports:
    1 8 33

    P.S. Oops! I just noticed that I have neglected to "undo" the piping
    of Heathfield's response through wc. Oh well, since said piping results in
    no reduction in the value or content of his words, I guess there is no
    problem there.
     
    Kenny McCormack, May 16, 2008
    #13
  14. Flash Gordon Guest

    Antoninus Twink wrote, On 15/05/08 23:37:
    > On 15 May 2008 at 22:30, Richard Heathfield wrote:
    >> Pietro Cerutti said:
    >>>> 2) as there is no true nor false in C (just 0 and not 0) what does
    >>>> a==b have for a value, if any?
    >>> 0 if a and be differ, some implementation-defined value other than 0 if
    >>> they're equal (usually 1).

    >> That's not how I read the Standard.

    >
    > Or just apply common sense. At the machine code level, there will be a
    > comparison instruction followed by one that sets a register to the
    > current value (0 or 1) of the zero flag.


    Apart from on processors with no such instruction, and I've used several
    where you could could not directly set a register on the basis of a
    condition flag, only branch on it. Having branched setting a register to
    all bits 1 (or writing all bits 1 to memory) would be just as easy as
    setting it to a value of 1. Why do you think that several BASICs use 0
    and -1?
    --
    Flash Gordon
     
    Flash Gordon, May 17, 2008
    #14
  15. Chris Dollin Guest

    wrote:

    > I don't remember the exact syntax, however I've heard that
    > return a==b;
    > is valid c (like in gcc does not complain)


    The "(like in gcc does not complain)" isn't an exact match to
    "is valid c". Be warned.

    > my questions:
    > 1) is this real C standard?


    Yes. `return` can have an expression operand. `a == b` is a legal
    expression, assuming `a` and `b` have been declared and have
    appropriate types (eg not structs). The result type is `int`,
    so the function this appears in had better have a compatible
    return-type.

    > 2) as there is no true nor false in C (just 0 and not 0) what does
    > a==b have for a value, if any?


    0 or 1; all the relational operators return 0 or 1.

    --
    /Questions? Answers! Answers? Questions!/ - Focus

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
     
    Chris Dollin, May 19, 2008
    #15
  16. In article <> Antoninus Twink <> writes:
    ....
    > Or just apply common sense. At the machine code level, there will be a
    > comparison instruction followed by one that sets a register to the
    > current value (0 or 1) of the zero flag.


    That is not much common sense on a machine that does not have a zero
    flag.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, May 19, 2008
    #16
  17. Chris Dollin Guest

    Dik T. Winter wrote:

    > In article <> Antoninus Twink <> writes:
    > ...
    > > Or just apply common sense. At the machine code level, there will be a
    > > comparison instruction followed by one that sets a register to the
    > > current value (0 or 1) of the zero flag.

    >
    > That is not much common sense on a machine that does not have a zero
    > flag.


    Or one that doesn't have an instruction to copy the flag into
    a register. (I can't think of a 1-instruction ARM sequence that
    does it; two-instruction sequences are of course trivial and
    branch-free. Maybe newer ARMs can do this, or my aged memory is
    dropping bts.)

    --
    "If I were you, I would go to the crackpots." /They Shall Have Stars/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
     
    Chris Dollin, May 19, 2008
    #17
    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. PvdK
    Replies:
    0
    Views:
    2,981
  2. wl
    Replies:
    2
    Views:
    587
    Dimitri Maziuk
    Mar 5, 2004
  3. Ganesh Gella
    Replies:
    4
    Views:
    363
    Stuart Gerchick
    Nov 12, 2004
  4. Seong-Kook Shin
    Replies:
    1
    Views:
    495
    Richard Bos
    Jun 18, 2004
  5. Greenhorn
    Replies:
    15
    Views:
    833
    Keith Thompson
    Mar 6, 2005
Loading...

Share This Page