0

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.

Radoslaw Krasimirow
  • 1,833
  • 2
  • 18
  • 28
  • 2
    And the followup to this question can be found here: http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const – Bill Lynch Apr 08 '15 at 14:59
  • possible duplicate of [Const before or const after?](http://stackoverflow.com/questions/5503352/const-before-or-const-after) I'm sure people will yell at me saying it's tagged C++, but in this context, it's an exact duplicate. – legends2k Apr 08 '15 at 15:04

3 Answers3

3

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

C11: 6.7 Declarations:

declaration-specifiers:

storage-class-specifier declaration-specifiersopt
type-specifier declaration-specifiersopt
type-qualifier declaration-specifiersopt

function-specifier declaration-specifiersopt
alignment-specifier declaration-specifiersopt

C11: 6.7.2 Type specifiers (p2):

[...] the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    I was looking through the standard, but then it was so human-unreadable that I gave up. +1 for finding it. – user12205 Apr 08 '15 at 15:13
2

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 to char const foo = 'a';). On some implementations, using const on both sides of the type (for instance, const char const) generates a warning but not an error.

user12205
  • 2,684
  • 1
  • 20
  • 40
0

They are both same. You declared 2 constants variables.