For those who like quoting chapter and verse...

Discussion in 'C Programming' started by Kenny McCormack, Feb 26, 2013.

  1. Given the assumption that anywhere (with two exceptions that I know of; see
    next} that you have stuff inside curly braces, you can remove the curly
    braces if there is one one "thing" inside them, ...

    The exceptions that I know of: 1) A function definition must start with "{".
    2) A switch statement must have "{" and "}", even if it has only one case.

    Anyway, given this assumption, why won't this program compile (see below):

    $ cat x.c
    int main(void) {
    int i = 1;
    if (i) int j;
    return 0;
    }
    $ gcc x.c
    x.c: In function ?main?:
    x.c:3:8: error: expected expression before ?int?
    $

    Note that the above *will* compile if I encloe "int j;" in curlies.

    --
    The motto of the GOP "base": You can't *be* a billionaire, but at least you
    can vote like one.
    Kenny McCormack, Feb 26, 2013
    #1
    1. Advertising

  2. On Feb 26, 3:37 pm, (Kenny McCormack)
    wrote:

    At the outset, IANAL, so I do not belong to the target audience (as
    indicated in your subject). That said, I suspect that those can
    quote chapter and verse in this group have most likely kill-filed
    you that they may not have seen your post at all.


    > Given the assumption that anywhere (with two exceptions that I know of; see
    > next} that you have stuff inside curly braces, you can remove the curly
    > braces if there is one one "thing" inside them, ...
    >
    > The exceptions that I know of: 1) A function definition must start with "{".
    > 2) A switch statement must have "{" and "}", even if it has only one case..
    >
    > Anyway, given this assumption, why won't this program compile (see below):
    >
    > $ cat x.c
    > int main(void) {
    > int i = 1;
    > if (i) int j;
    > return 0;}
    >
    > $ gcc x.c
    > x.c: In function ?main?:
    > x.c:3:8: error: expected expression before ?int?
    > $
    >
    > Note that the above *will* compile if I encloe "int j;" in curlies.
    >


    One may have to get down to the grammar and see what the productions
    say.

    It is apparent that while a declaration such as -

    int i = 0, j = 0;

    - is allowed outside of any function, a statement such as -

    i = j;

    - is not allowed outside any function. So, there must be something in
    the grammar that distinguishes a declaration from a statement.

    I suspect the grammar might say that the statement following an if
    condition cannot be composed of just a declaration alone, but must
    either be a block or a statement.

    - Anand
    Anand Hariharan, Feb 26, 2013
    #2
    1. Advertising

  3. Kenny McCormack

    Öö Tiib Guest

    On Tuesday, 26 February 2013 23:37:49 UTC+2, Kenny McCormack wrote:
    > int main(void) {
    > int i = 1;
    > if (i) int j;
    > return 0;
    > }


    It is C++, where declaration of variable may
    have side effects. Your code is out of topic here being C++.

    In C it is illegal (at least in C99 it was):

    (6.8) statement
    It can be
    expression-statement
    compound-statement
    and several others none is declaration.

    (6.8.2) compound statement
    it is
    { declarations and statements in mix }

    (6.8.3) expression-statement
    optional expression and semicolon.
    expression is disallowed to be declaration.

    (6.8.4) selection-statement
    it is
    if (expression) statement
    but is not such thing as
    if (expression) declaration

    So no way your code should work in C99. Also it is pointless in C anyway.
    Öö Tiib, Feb 27, 2013
    #3
  4. Kenny McCormack

    Shao Miller Guest

    On 2/26/2013 16:37, Kenny McCormack wrote:
    > Given the assumption that anywhere (with two exceptions that I know of; see
    > next} that you have stuff inside curly braces, you can remove the curly
    > braces if there is one one "thing" inside them, ...
    >
    > The exceptions that I know of: 1) A function definition must start with "{".


    It starts like a function declaration, but must have parentheses, then
    has a compound statement, yes.

    > 2) A switch statement must have "{" and "}", even if it has only one case.
    >


    You are mistaken about this. 'switch' doesn't require them.

    > Anyway, given this assumption, why won't this program compile (see below):
    >
    > $ cat x.c
    > int main(void) {
    > int i = 1;
    > if (i) int j;
    > return 0;
    > }
    > $ gcc x.c
    > x.c: In function ?main?:
    > x.c:3:8: error: expected expression before ?int?
    > $
    >
    > Note that the above *will* compile if I encloe "int j;" in curlies.
    >


    'if' requires a statement, where you have a declaration. A compound
    statement can have declarations and statements inside.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Feb 27, 2013
    #4
  5. Kenny McCormack

    James Kuyper Guest

    On 02/26/2013 06:30 PM, Anand Hariharan wrote:
    > On Feb 26, 3:37 pm, (Kenny McCormack)
    > wrote:
    >
    > At the outset, IANAL, so I do not belong to the target audience (as
    > indicated in your subject). That said, I suspect that those can
    > quote chapter and verse in this group have most likely kill-filed
    > you that they may not have seen your post at all.


    Correct.

    >> Given the assumption that anywhere (with two exceptions that I know of; see
    >> next} that you have stuff inside curly braces, you can remove the curly
    >> braces if there is one one "thing" inside them, ...


    His assumption is incorrect when the curly braces don't begin and
    terminate a compound statement. For example, you cannot drop the curly
    braces in the following:

    int array[1] = {1};

    Even when the curly brackets do mark the beginning and end of a compound
    statement, his assumption still can fail if the only "thing" inside a
    compound statement is a declaration. That's because removing the curly
    brackets would replace a statement with with a declaration. Most
    locations where compound statements are allowed are NOT locations where
    declarations are allowed (see the grammar summary in A.2.3), and that's
    what went wrong in his example code below. The only exceptions are
    within another compound statement (6.8.2p1), and in function definitions
    (as he notes below).

    >> The exceptions that I know of: 1) A function definition must start with "{".
    >> 2) A switch statement must have "{" and "}", even if it has only one case.


    No, there's no such requirement. Neither of the following switch
    statements is a syntax error, nor do they violate any constraints.

    switch(i++) j = 3;
    switch(j) case 5: i = 4;

    Those statements are functionally equivalent to:

    i++;
    if(j==5) i = 4;

    which is a much better way to write such code - but there's nothing
    wrong with those switch() statements, as far as the C standard is
    concerned, just because they are don't have any curly brackets.

    If the compound statement controlled by a switch() contained only a
    single declaration, removing the curly brackets would be a syntax error,
    for the same reason as for the if() statement below .

    >> Anyway, given this assumption, why won't this program compile (see below):


    Given that assumption, the program should compile. The problem is with
    the assumption, which is false.

    >> $ cat x.c
    >> int main(void) {
    >> int i = 1;
    >> if (i) int j;


    In particular, the grammar rules for if statements say:

    if ( expression ) statement
    or
    if ( expression ) statement else statement


    "int j;" is a declaration, so it it doesn't match either form.

    >> return 0;}
    >>
    >> $ gcc x.c
    >> x.c: In function ?main?:
    >> x.c:3:8: error: expected expression before ?int?
    >> $
    >>
    >> Note that the above *will* compile if I encloe "int j;" in curlies.


    {int j;} is a compound statement, and therefore a statement (6.8p1), so
    it matches the first grammar rule for if statements.

    > One may have to get down to the grammar and see what the productions
    > say.


    Definitely. Talking about "stuff" and "things" is just not sufficiently
    precise to resolve questions like this.

    > It is apparent that while a declaration such as -
    >
    > int i = 0, j = 0;
    >
    > - is allowed outside of any function, a statement such as -
    >
    > i = j;
    >
    > - is not allowed outside any function. So, there must be something in
    > the grammar that distinguishes a declaration from a statement.


    Lots of things, mostly in section 6.8.

    > I suspect the grammar might say that the statement following an if
    > condition cannot be composed of just a declaration alone, but must
    > either be a block or a statement.


    Actually, no - it's simpler than that - a compound statement is a
    statement (6.8p1), so "statement" is sufficient.

    All compound statements are blocks (6.8.2), but so are selection
    statements and their associated sub statements (6.8.4p3), iteration
    statements and the bodies of their loops (6.8.5p5), even if none of
    those things is a compound statement.

    Therefore, it actually works the other way around: in the statement

    if(i) j = 3;

    the sub-statement "j=3" is a block, because of it's position in the if
    statement.
    James Kuyper, Feb 27, 2013
    #5
  6. Kenny McCormack

    army1987 Guest

    On Tue, 26 Feb 2013 19:45:20 -0500, James Kuyper wrote:

    > No, there's no such requirement. Neither of the following switch
    > statements is a syntax error, nor do they violate any constraints.
    >
    > switch(i++) j = 3;
    > switch(j) case 5: i = 4;
    >
    > Those statements are functionally equivalent to:
    >
    > i++;
    > if(j==5) i = 4;
    >
    > which is a much better way to write such code - but there's nothing
    > wrong with those switch() statements, as far as the C standard is
    > concerned, just because they are don't have any curly brackets.


    Interesting. I might use that should I ever decide to participate in the
    IOCCC.



    --
    [ T H I S S P A C E I S F O R R E N T ]
    Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
    -- fathermckenzie di it.cultura.linguistica.italiano
    <http://xkcd.com/397/>
    army1987, Feb 27, 2013
    #6
  7. Kenny McCormack

    Shao Miller Guest

    On 2/27/2013 09:44, army1987 wrote:
    > On Tue, 26 Feb 2013 19:45:20 -0500, James Kuyper wrote:
    >
    >> No, there's no such requirement. Neither of the following switch
    >> statements is a syntax error, nor do they violate any constraints.
    >>
    >> switch(i++) j = 3;
    >> switch(j) case 5: i = 4;
    >>
    >> Those statements are functionally equivalent to:
    >>
    >> i++;
    >> if(j==5) i = 4;
    >>
    >> which is a much better way to write such code - but there's nothing
    >> wrong with those switch() statements, as far as the C standard is
    >> concerned, just because they are don't have any curly brackets.

    >
    > Interesting. I might use that should I ever decide to participate in the
    > IOCCC.
    >


    Yeah. Suppose you wish to hook 'return' statements:

    #define return \
    switch (1) \
    while (1) \
    if (0) { \
    default: \
    do_something(); \
    } \
    else \
    return

    This 'switch' will invoke the 'default' case, then 'do_something()',
    then continue the 'while', which then invokes the 'if', whose condition
    is false, so then invokes the 'else', which performs the 'return'.
    (Note the trailing space after the final 'return'.)

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Feb 27, 2013
    #7
  8. Kenny McCormack

    James Kuyper Guest

    On 02/26/2013 07:45 PM, James Kuyper wrote:
    > On 02/26/2013 06:30 PM, Anand Hariharan wrote:
    >> On Feb 26, 3:37 pm, (Kenny McCormack)
    >> wrote:

    ....
    >>> Given the assumption that anywhere (with two exceptions that I know of; see
    >>> next} that you have stuff inside curly braces, you can remove the curly
    >>> braces if there is one one "thing" inside them, ...

    ....
    > brackets would replace a statement with with a declaration. Most
    > locations where compound statements are allowed are NOT locations where
    > declarations are allowed (see the grammar summary in A.2.3), and that's
    > what went wrong in his example code below. The only exceptions are
    > within another compound statement (6.8.2p1), and in function definitions
    > (as he notes below).
    >
    >>> The exceptions that I know of: 1) A function definition must start with "{".


    I just re-read that, and realized that I got a little confused while
    editing it. Function definitions are not exceptions to my assertion that
    "Most locations where compound statements are allowed are NOT locations
    where declarations are allowed". It is precisely because of that fact
    that they ARE exceptions to his assumption.
    James Kuyper, Feb 27, 2013
    #8
  9. On Feb 26, 11:30 pm, Anand Hariharan
    <> wrote:
    > On Feb 26, 3:37 pm, (Kenny McCormack)
    > wrote:
    >
    > At the outset, IANAL, so I do not belong to the target audience (as
    > indicated in your subject).  That said, I suspect that those can
    > quote chapter and verse in this group have most likely kill-filed
    > you that they may not have seen your post at all.


    Everything I've seen recently looks perfectly sensible. Perhaps its
    time to pop him out of the kill files for a while.

    > > Given the assumption that anywhere (with two exceptions that I know of;see
    > > next} that you have stuff inside curly braces, you can remove the curly
    > > braces if there is one one "thing" inside them, ...

    >
    > > The exceptions that I know of: 1) A function definition must start with"{".
    > > 2) A switch statement must have "{" and "}", even if it has only one case.

    >
    > > Anyway, given this assumption, why won't this program compile (see below):

    >
    > > $ cat x.c
    > > int main(void) {
    > > int i = 1;
    > > if (i) int j;
    > > return 0;}

    >
    > > $ gcc x.c
    > > x.c: In function ?main?:
    > > x.c:3:8: error: expected expression before ?int?
    > > $

    >
    > > Note that the above *will* compile if I encloe "int j;" in curlies.

    >
    > One may have to get down to the grammar and see what the productions
    > say.
    >
    > It is apparent that while a declaration such as -
    >
    > int i = 0, j = 0;
    >
    > - is allowed outside of any function, a statement such as -
    >
    > i = j;
    >
    > - is not allowed outside any function. So, there must be something in
    > the grammar that distinguishes a declaration from a statement.
    >
    > I suspect the grammar might say that the statement following an if
    > condition cannot be composed of just a declaration alone, but must
    > either be a block or a statement.
    Nick Keighley, Mar 9, 2013
    #9
    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. BinnuChowdary
    Replies:
    1
    Views:
    537
    Swanand Mokashi
    May 1, 2006
  2. BinnuChowdary
    Replies:
    0
    Views:
    411
    BinnuChowdary
    May 2, 2006
  3. BinnuChowdary
    Replies:
    1
    Views:
    546
    =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
    May 2, 2006
  4. Steven T. Hatton

    #include verse class class_name

    Steven T. Hatton, Apr 8, 2004, in forum: C++
    Replies:
    14
    Views:
    589
    Steven T. Hatton
    Apr 10, 2004
  5. The Natural Philosopher

    Hmm. Anyone go chapter and verse on this one?

    The Natural Philosopher, Oct 17, 2007, in forum: Javascript
    Replies:
    36
    Views:
    307
    Darko
    Nov 7, 2007
Loading...

Share This Page