From External Variables Wiki:
If neither the extern keyword nor an initialization value are present, the statement can be either a declaration or a definition. It is up to the compiler to analyse the modules of the program and decide.
I was not able to fully grasp the meaning of this statement with respect to C. For example, does it imply that:
int i;
is not necessarily a declaration (as I have been assuming until now), but could be a definition as well (by definition of Definition & Declaration on the same webpage, no puns intended)?
In a nutshell, is the above statement: a. just a declaration, or b. declaration + definition?
Reference: Variable declaration and definition
Summary of answers received:
Declaration Definition Tentative Definition Initialized int i; (inside a block) Yes Yes No No int i=5; (inside a block) Yes Yes No Yes(to 5) int i; (otherwise) Yes No Yes Yes(to 0) extern int i; Yes No No No All definitions are declarations but not vice-versa.
In the context of variables:
A declaration of a variable is a statement which describes how this variable looks like. So:
in global scope translates to: "somewhere in the code, there's a variable called
x
which has typeint
and extern linkage. A declaration is necessary before you ever refer tox
. (The same goes to function declarations.)A definition is a statement which creates an instance of this variable. So:
in global scope creates a single variable of type
int
with extern linkage. So if you'd put that line in a header, every translation unit including that header would try to create its own copy ofx
, which is undesirable - that's why we only have declarations in header files. The same goes to functions: if you provide the function body, it's a definition.Also, formally, every definition is a kind of declaration, as it also has to specify how this variable/function looks like - so if a definition already exists in a given scope, you don't need any additional declarations to use it.
From the C99 spec:
So this is one case in which a simple declaration without an initializer can be a declaration.
As C uses the terms:
A "definition" creates something (which occupies some sort of memory). It also describes something. This means a "definition" is also a "declaration".
A "declaration" just describes something. The idea is that the compiler needs to know how to build the code that uses the thing defined elsewhere. Later, the linker then links the use to the something.
Declarations allow you to compile code and link it (later) as a separate step.
Assuming it's at file scope it's a 'tentative definition'. From 6.9.2/2 "External object definitions":
This means that it would be valid to also have the following in the translation unit:
since that declaration has an explicit initializer, it's the definition of the variable
i
.As far as if the declaration is in a block scope, the standard says the following (6.2.2/2 "Linkages of identifiers"):
So in block scope, the declaration would be a definition as well.
The C standard says that
Definitions encompass declarations, i.e., every definition is necessarily a declaration, so it doesn’t make sense to say that
is not a declaration. It is a declaration which also happens to be a definition. Or, it is a definition, hence a declaration.