Are declarators optional?

C

chandanlinster

In K&R (page 210, A8), "declaration" is defined to be:

declaration:
declaration-specifiers init-declarators-list(opt);

Later (3rd paragraph), it says that, "empty declarations are not
permitted".

According to the former definition of "declaration" (which specifies
"declarators" as
being optional) "empty declarations" should be permitted.

So i tried out the following program ...

/***************************/
#include <stdio.h>
#include <stdlib.h>

int main(void) {
int;

putchar('\n');
exit(0);
}
/**************************/

.... and compiled it using ...
$cc prog.c -ansi -pedantic
prog.c: In function 'main':
prog.c:5: warning: useless type name in empty declaration

So i got only a warning (not an error).

The final question would be:- "Are declarators(or "identifiers")
optional?

PS: I am using gcc 4.1 on Linux 2.6
 
B

Ben Bacarisse

chandanlinster said:
In K&R (page 210, A8), "declaration" is defined to be:

declaration:
declaration-specifiers init-declarators-list(opt);

Later (3rd paragraph), it says that, "empty declarations are not
permitted".

According to the former definition of "declaration" (which specifies
"declarators" as
being optional) "empty declarations" should be permitted.

So i tried out the following program ...

/***************************/
#include <stdio.h>
#include <stdlib.h>

int main(void) {
int;

putchar('\n');
exit(0);
}
/**************************/

... and compiled it using ...
$cc prog.c -ansi -pedantic
prog.c: In function 'main':
prog.c:5: warning: useless type name in empty declaration

So i got only a warning (not an error).

As far as the standard is concerned, there are only diagnostics.
There is not distinction between warnings and errors.
The final question would be:- "Are declarators(or "identifiers")
optional?

I have not checked the standard because it is a curious case where the
compiler wins either way!

(a) If they are allowed (by the standard) then the compiler is still
permitted to issue a diagnostic provided it translates the program
(which it did).

(b) If they are not allowed, the compiler must issue a diagnostic
(which it did) but it *may*, if it so wishes, continue to translate
the program.

You could ask if the standard defines the meaning of:

#include <stdlib.h>

struct s1 { int x; int i; };
struct s2 { int x; int; };

int main(void)
{
return sizeof(struct s1) == sizeof(struct s2) ?
EXIT_SUCCESS : EXIT_FAILURE;
}

but then I'd have to look it up.
 
G

Guest

chandanlinster said:
In K&R (page 210, A8), "declaration" is defined to be:

declaration:
declaration-specifiers init-declarators-list(opt);

Later (3rd paragraph), it says that, "empty declarations are not
permitted".

According to the former definition of "declaration" (which specifies
"declarators" as
being optional) "empty declarations" should be permitted.

Grammatically, they are.
So i tried out the following program ...

/***************************/
#include <stdio.h>
#include <stdlib.h>

int main(void) {
int;

This is syntactically valid, but a constraint violation. To simplify a
bit: a declaration must declare at least one name. This name could be
a declarator, but it could also be a tag name,
struct s;
or an enumeration constant.
enum { zero };
putchar('\n');
exit(0);
}
/**************************/

... and compiled it using ...
$cc prog.c -ansi -pedantic
prog.c: In function 'main':
prog.c:5: warning: useless type name in empty declaration

So i got only a warning (not an error).

As long as at least one diagnostic is issued, it's all good. The only
time a compiler /must/ reject a program is if it contains an #error
directive.
The final question would be:- "Are declarators(or "identifiers")
optional?

In some cases, yes. In others, no.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top