I am new in C programming and I wondered what is the difference between these two declarations:
const int a;
and
int const a;
Both are well accepted by the compiler.
I am new in C programming and I wondered what is the difference between these two declarations:
const int a;
and
int const a;
Both are well accepted by the compiler.
There is no difference. Both are same. int
and const
both are declaration specifiers and they can occur in any order. Grammar says all about it
declaration-specifiers:
storage-class-specifier declaration-specifiersopt
type-specifier declaration-specifiersopt
type-qualifier declaration-specifiersopt
function-specifier declaration-specifiersopt
alignment-specifier declaration-specifiersopt
[...] the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.
No difference.
From Wikipedia:
For simple non-pointer data types, applying the
const
qualifier is straightforward. It can go on either side of the type for historical reasons (that is,const char foo = 'a';
is equivalent tochar const foo = 'a';
). On some implementations, usingconst
on both sides of the type (for instance,const char const
) generates a warning but not an error.