What is the default type for C const?

2020-04-05 07:15发布

I was writing some C code, and noticed what I thought to be an error, but was not. I had the following type declaration statement.

const fee;

However, it was uncaught initially because the compiler and I didn't catch it. So I was curious as to why C allows this and what is the default type.

标签: c types
1条回答
霸刀☆藐视天下
2楼-- · 2020-04-05 07:32

Only the original version of C language standard (ANSI/ISO C89/90) allows this. Such variable declaration defaults to type int in accordance with "implicit int" rule. That rule was present in C since the beginning of time. That's just how the language was originally defined.

Note that the declaration-specifiers portion of a declaration cannot be omitted completely, e.g. a mere

fee;

does not declare an int variable. It is illegal even in the original C. But once you add some sort of declaration specifier or qualifier, the declaration becomes legal and defaults to int type, as in

static fee;
const fee;
register fee;

However, all this is illegal in C99 and in later versions of language, since these versions of language specification outlawed "implicit int".

查看更多
登录 后发表回答