Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are globals considered as statements?

Tags:

c

global

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

like image 719
Random Avatar asked Dec 18 '25 05:12

Random


1 Answers

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.

like image 162
Eric Postpischil Avatar answered Dec 19 '25 19:12

Eric Postpischil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!