Reputation: 8187
"The const and volatile qualifiers may precede any declaration."
I saw this statement marked as true in an online test series. But in standard C(89) I can see
declaration:
declaration-specifiers init-declarator-listopt ;
declaration-specifiers:
storage-class-specifier declaration-specifiersopt
type-specifier declaration-specifiersopt
type-qualifier declaration-specifiersopt
function-specifier declaration-specifiersopt
init-declarator-list:
init-declarator
init-declarator-list , init-declarator
init-declarator:
declarator
declarator = initializer
which seems from above that this statement can come out false for a few declaration.
Please help!
EDIT: I know this is not valid for ISO C89 or above, but please suggest for ANSI, so that the education authority be informed about the question bug with some proof.
Upvotes: 2
Views: 361
Reputation: 45057
You indeed can place const
or volatile
before any declaration without violating C's grammar rules. This in no way implies that such a construct has meaning, won't be ignored outright, or won't trigger a compile error for some other reason. It only means that it won't trigger a syntax error.
Section 3.5.3 of the C89 spec states
If the specification of a function type includes any type qualifiers, the behavior is undefined.
This means that it's perfectly legal to declare a function as const
or volatile
, as long as you don't actually call that function. If you try to call it, there's no telling what would happen. This is one of several things you can do in C that are technically legal syntax but are completely pointless (like the statement 1 == 3;
or x + 2;
).
To clarify your comments in your edit, be aware that the terms "ANSI C" and "C89" refer to the same thing. There are both ANSI and ISO standards for C that differ only in formatting. The content of those standards is what is commonly called "C89" or "C90" (to distinguish it from C99, which ANSI later standardized as well). When you say "I know this is not valid for ISO C89 or above", your statement includes "ANSI C" as well.
Upvotes: 0
Reputation: 183873
type-qualifier declaration-specifiers(opt)
allows a type qualifier, such as const
or volatile
, followed by declaration-specifiers. Those following declaration-specifiers can be a function declaration.
Upvotes: 3