Strange GCC warning on storage class and type

前提是你 提交于 2019-12-02 07:56:41

It is because you define TOS in the global scope, which need you to define the type of TOS(it is an declaration), if no type was given, by default it is int.

This will cause an conflicting type error,

char x;
x = 0;

The correct way to forward a variable in a header file would be

extern int TOS;

without the extern this could otherwise result that TOS is allocated in several compilation units (.o files).

You'd then give a definition in one .c file as

int TOS;

This would then reserve space for it and since it is a variable in global scope it also would initialize it to 0. If you want to make this initialization explicit or if you want it to be to another value than 0, the correct syntax for initialization (and not assignment) is

int TOS = 54;

Modern C doesn't allow the syntax that you seem to have inherited from somewhere, namely a definition of a global variable with implicit type int.

TOS=0 is not an assignment, it's a declaration with an initializer (i.e: a definition). int TOS; is a tentative definition with external linkage. When the linker links several translation units together, it collapses the corresponding object (=memory for a variable). As said elsewhere, the default type of int is a C89 feature absent from later editions of the standard.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!