I am new in C programming and I wondered what is the difference between these two declarations:
我是C编程的新手,我想知道这两个声明之间有什么区别:
const int a;
and
int const a;
Both are well accepted by the compiler.
两者都被编译器很好地接受了。
3 个解决方案
#1
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
没有区别。两者都是一样的。 int和const都是声明说明符,它们可以按任何顺序出现。语法说的都是关于它的
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-specifiersoptstorage-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.
[...]类型说明符可以按任何顺序出现,可能与其他声明说明符混合。
#2
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 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.对于简单的非指针数据类型,应用const限定符很简单。由于历史原因,它可以在类型的任何一侧出现(即,const char foo ='a';等同于char const foo ='a';)。在某些实现中,在类型的两侧使用const(例如,const char const)会生成警告但不会生成错误。
#3
0
They are both same. You declared 2 constants variables.
它们都是一样的。你声明了2个常量变量。
#1
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
没有区别。两者都是一样的。 int和const都是声明说明符,它们可以按任何顺序出现。语法说的都是关于它的
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-specifiersoptstorage-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.
[...]类型说明符可以按任何顺序出现,可能与其他声明说明符混合。
#2
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 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.对于简单的非指针数据类型,应用const限定符很简单。由于历史原因,它可以在类型的任何一侧出现(即,const char foo ='a';等同于char const foo ='a';)。在某些实现中,在类型的两侧使用const(例如,const char const)会生成警告但不会生成错误。
#3
0
They are both same. You declared 2 constants variables.
它们都是一样的。你声明了2个常量变量。