Home > OS >  Are globals considered statements?
Are globals considered statements?

Time:07-08

This might be a simple question, but are globals, and anything written outside of a function (structs, enums, functions...), considered statements? If so, are all of them statements, or only some of them?

CodePudding user response:

In the formal C grammar, a translation-unit, which is the sequence of tokens resulting from reading one source file and applying “preprocessing” to it (including macro replacement and inclusion of files via #include), consists of function-definition and declaration items. These are not statements.

A function-definition both declares a function (makes its name known to the compiler) and defines it (provides code for the body of a function, in a compound-statement that starts with { and ends with }).

Inside the compound-statement there are declaration and statement items. A declaration says something about some identifier (a name used for some an object, function, structure tag, typedef name, or member of a structure, union, or enumeration).

So, when a variable is declared outside of any function, it is in a declaration, not in a statement.

Some declarations are also definitions, but this distinction is not made directly in the formal grammar.

  • Related