Is this program legal C89?

Discussion in 'C Programming' started by Army1987, Jul 24, 2007.

  1. Army1987

    Army1987 Guest

    Is this program legal C89?

    /* no headers included */
    int main(void)
    {
    if (sizeof (exit(0), 0),
    ((void (* )(int))&exit)(
    (puts((const char *)"hello, world"), 0)
    ),
    0) {
    }
    }

    The the first operand of the first comma serves to implicitly
    declare exit() as returning an int, but it is never called as
    such. Does that wrong implicit declaration cause UB by itself,
    even if the function is eventually called with the right type?

    --
    Army1987 (Replace "NOSPAM" with "email")
    "Never attribute to malice that which can be adequately explained
    by stupidity." -- R. J. Hanlon (?)
    Army1987, Jul 24, 2007
    #1
    1. Advertising

  2. Army1987

    Eric Sosman Guest

    Army1987 wrote On 07/24/07 07:01,:
    > Is this program legal C89?


    Legal. Repellent, but legal.

    > /* no headers included */
    > int main(void)
    > {
    > if (sizeof (exit(0), 0),
    > ((void (* )(int))&exit)(
    > (puts((const char *)"hello, world"), 0)
    > ),
    > 0) {
    > }
    > }
    >
    > The the first operand of the first comma serves to implicitly
    > declare exit() as returning an int, but it is never called as
    > such. Does that wrong implicit declaration cause UB by itself,
    > even if the function is eventually called with the right type?


    The type of the function pointer used at the call
    site matches the type of the called function: it's legal.

    --
    Eric Sosman, Jul 24, 2007
    #2
    1. Advertising

  3. Army1987 wrote:
    > Is this program legal C89?
    >
    > /* no headers included */
    > int main(void)
    > {
    > if (sizeof (exit(0), 0),
    > ((void (* )(int))&exit)(
    > (puts((const char *)"hello, world"), 0)
    > ),
    > 0) {
    > }
    > }
    >
    > The the first operand of the first comma serves to implicitly
    > declare exit() as returning an int, but it is never called as
    > such. Does that wrong implicit declaration cause UB by itself,
    > even if the function is eventually called with the right type?


    The only relevant paragraph in a C89 draft I'm looking at is
    3.1.2.6 Compatible type and composite type
    "All declarations that refer to the same object or function shall have
    compatible type; otherwise the behavior is undefined."
    Considering the implementation of the standard library contains a definition
    (which is a declaration) of exit(), and your program's implicit declaration
    is not compatible with it, I believe the behaviour is undefined, but
    whether 3.1.2.6 is meant to apply to standard library functions is not
    entirely clear. C99 clears it up with an explicit prohibition against
    incorrect declarations of standard library functions:
    7.1.3p2:
    "No other identifiers are reserved. If the program declares or defines an
    identifier in a context in which it is reserved (other than as allowed by
    7.1.4), or defines a reserved identifier as a macro name, the behavior is
    undefined."
    so if you make the implicit declarations explicit, it's still definitely
    invalid C99, but I have been told this text is not present in C89.
    Harald van =?UTF-8?B?RMSzaw==?=, Jul 24, 2007
    #3
    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. Replies:
    13
    Views:
    6,410
    Dave Thompson
    Dec 20, 2004
  2. j

    long long extended type in c89?

    j, Aug 23, 2003, in forum: C Programming
    Replies:
    5
    Views:
    672
    Peter Shaggy Haywood
    Aug 26, 2003
  3. Daniel Schüle

    {0} oder {0, } C89 C99

    Daniel Schüle, Nov 12, 2003, in forum: C Programming
    Replies:
    6
    Views:
    322
  4. Servé Lau

    %ls C89?

    Servé Lau, Nov 29, 2003, in forum: C Programming
    Replies:
    5
    Views:
    538
    Dan Pop
    Dec 1, 2003
  5. G Patel
    Replies:
    1
    Views:
    547
Loading...

Share This Page