Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are extra semicolons allowed inside struct declarations?

This question is by mere curiosity. It is not about the empty struct.

I just stumbled over an interesting typo of the kind

struct {
     int member1; /*comment*/ ; /* <-- note the ';' */
     int member2;
} variable[] = { /* initializers */ };

which the compiler (xc32, derived from gcc) accepted without any complaints. Of course, I corrected this but the software was running smoothly before and after the correction and the additional ; seemingly causes no problems. I then tried various lengthes of ;;; in he struct definition an they seem to make no difference neither to functionality nor to sizeof. So in a struct any sequence of ;;;; seems to be equivalent to a single ;.

I couldn't find anything about such "empty members" of a struct/union in the specification, neither that they are allowed nor that they are disallowed. To me it seems as if the grammar rejected them. This contrasts to "empty declarations" ; at the top level of a compilation unit, which the standard clearly forbids and the "null" statement ; in functions which is a clearly allowed language feature.

Does anyone know about this behaviour? Is it compiler specific or does the C specification somehow tolerate such empty struct members?

like image 709
N. Paul Avatar asked Mar 28 '18 08:03

N. Paul


People also ask

Do structs need semicolons?

A semicolon after a close brace is mandatory if this is the end of a declaration. In case of braces, they have used in declarations of class, enum, struct, and initialization syntax. At the end of each of these statements, we need to put a semicolon.

Why do we need to give a semicolon After a structure declaration?

You have to put the semicolon there so the compiler will know whether you declared any instances or not. This is a C compatibility thing. Doesn't even need the name or constituents: class { } waldo; is legal.

Are semicolons required in Java?

Every statement in Java must end with a semicolon as per the basics. However, unlike other languages, almost all statements in Java can be treated as expressions. However, there are a few scenarios when we can write a running program without semicolons.


2 Answers

The standard doesn't talk about that, it's just a gcc tolerance. See 6.7.2.1:

struct-or-union-specifier:
              struct-or-union identifieropt { struct-declaration-list }
              struct-or-union identifier
struct-or-union:
             struct
             union
struct-declaration-list:
             struct-declaration
             struct-declaration-list struct-declaration
struct-declaration:
             specifier-qualifier-list struct-declarator-listopt ;
             static_assert-declaration
specifier-qualifier-list:
            type-specifier specifier-qualifier-listopt
            type-qualifier specifier-qualifier-listopt

(type-specifier and type-qualifier can't be empty, see the related sections in the standard for details.)


Some compilers, like gcc, tolerate extra semi-colons, but -Wpedantic option reveals that it's only a tolerance:

struct foo {
    int a;
    ;;;
};

int main() {
    ;;;
}

With -pedantic option gcc complains, not on the main empty statements, but on the extra semicolons of the structure declaration.

<source>:3:5: warning: extra semicolon in struct or union specified [-Wpedantic]
     ;;;
     ^
<source>:3:6: warning: extra semicolon in struct or union specified [-Wpedantic]
     ;;;
      ^
<source>:3:7: warning: extra semicolon in struct or union specified [-Wpedantic]
     ;;;

Other compilers may not be that friendly, so the typo must be fixed, since it doesn't bring anything useful.

like image 24
5 revs, 2 users 67% Avatar answered Oct 08 '22 16:10

5 revs, 2 users 67%


The syntax is specified in C11 6.7.2.1

struct-declaration:
specifier-qualifier-list struct-declarator-listopt ;
static_assert-declaration

There is 1 semicolon at the end, so that's the only allowed syntax. You cannot skip the semicolon, you can't add extra ones. And that's that.

(You can however have a static assert inside a struct declaration, from C11.)

like image 127
Lundin Avatar answered Oct 08 '22 15:10

Lundin