#include <stdio.h>
extern int x;
int main()
{
int x;
x = 1;
printf("%d", x);
}
#include <stdio.h>
int main()
{
extern int x;
int x;
x = 1;
printf("%d", x);
}
I am learning about extern keyword and I was told that the word extern is used to declare variable and compiler will not allocate for this variable.
But when I write 2 codes, the first code run normally and the second has an error.
Please help me explain why it has this difference. Thanks.
Conflicting Declarations in Same Scope
When extern int x; appears outside a function, it declares x at file scope. Then, when int x; appears inside the function, it declares a new instance of x that is unrelated to the earlier extern int x;. This is allowed by the C language, so the compiler does not complain about it.
When extern int x; appears inside the function, it declares x at block scope. Then, when int x; appears after it, it attempts to declare a different x in the same scope. This is not allowed by the C standard, so the compiler reports an error.
The extern keyword is not particularly relevant here—the error is caused by the fact that there are two conflicting declarations of the same identifier. For example:
char c;
int main(void)
{
char d;
int c; // Allowed, new declaration in new scope.
int d; // Not allowed, conflicting declaration in same scope.
}
Rules About Declarations
The rules about C declarations have some irregularities due to the history of C development. Consider these declarations at file scope:
extern int x;
int x;
The first declaration does (or does not do) several things:
It says x is an identifier for an int.
It says x has external linkage, meaning it can be made (during linking of the object modules) to refer to an object named x declared somewhere else.
It does not define an int.
For the second declaration:
It says x is an identifier for an int.
It says x has external linkage (because external is the default for declarations for objects without a storage class specifier like static outside functions).
It defines an int. (It is actually a tentative definition, but we will not deal with that here.)
Both of these declarations say x is an identifier for an int and has external linkage. The difference between them is the first does not define an object (it merely says x is a name for an object defined somewhere else) and the second does define an int (so it is the somewhere else). So these declarations do not conflict and are allowed.
On other hand, consider these same declarations inside a function. Then they are at block scope.
Then extern int x; has the same meaning as above: x is an identifier with external linkage for an object defined elsewhere.
But int x; has a different meaning. Instead of saying x has external (or internal) linkage, it says x has no linkage, because no linkage is the default for declarations in block scope. This creates a conflict because C 2018 6.7 3 says an identifier with no linkage shall not be declared more than once in the same scope (and name space, not addressed here) except for typedef names and tags with certain conditions.
Scopes
C has four kinds of scopes:
File scope is for declarations outside of functions and lasts to the end of the source file being compiled.
Block scope is for declarations inside functions and lasts until the end of a block (discussed below).
Function prototype scope is for declarations in the parameters of function prototypes. (For example, in void foo(int n, float a[n][n]);, n and a have function prototype scope.)
Function scope is for labels to be used in goto statements.
A compound statement is a list of declarations and statements inside { and }. Each compound statement is a block, which creates a new scope for declarations. The main body of a function is a compound statement that is a block, and it may have additional compound statements inside it, each starting a new scope.
Blocks are also created by switch, if, do, while, and for statements, but they are largely unimportant for the first four of those, as only the for statement provides an opportunity for further declarations. For example, in a for statement, you can write for (int x = 3; x < 20; ++x), and that creates a new instance of x because the for statement starts a new block.
The meaning of both eludes me.
A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:
extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for type declarations
A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:
int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};
A definition can be used in the place of a declaration.
An identifier can be declared as often as you want. Thus, the following is legal in C and C++:
double f(int, double);
double f(int, double);
extern double f(int, double); // the same as the two above
extern double f(int, double);
However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.
Since the debate what is a class declaration vs. a class definition in C++ keeps coming up (in answers and comments to other questions) , I'll paste a quote from the C++ standard here.
At 3.1/2, C++03 says:
A declaration is a definition unless it [...] is a class name declaration [...].
3.1/3 then gives a few examples. Amongst them:
[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b [...]
struct S; // declares S
—end example
To sum it up: The C++ standard considers struct x; to be a declaration and struct x {}; a definition. (In other words, "forward declaration" a misnomer, since there are no other forms of class declarations in C++.)
Thanks to litb (Johannes Schaub) who dug out the actual chapter and verse in one of his answers.
From the C++ standard section 3.1:
A declaration introduces names into a translation unit or redeclares names introduced by previous
declarations. A declaration specifies the interpretation and attributes of these names.
The next paragraph states (emphasis mine) that a declaration is a definition unless...
... it declares a function without specifying the function’s body:
void sqrt(double); // declares sqrt
... it declares a static member within a class definition:
struct X
{
int a; // defines a
static int b; // declares b
};
... it declares a class name:
class Y;
... it contains the extern keyword without an initializer or function body:
extern const int i = 0; // defines i
extern int j; // declares j
extern "C"
{
void foo(); // declares foo
}
... or is a typedef or using statement.
typedef long LONG_32; // declares LONG_32
using namespace std; // declares std
Now for the big reason why it's important to understand the difference between a declaration and definition: the One Definition Rule. From section 3.2.1 of the C++ standard:
No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.
Declaration: "Somewhere, there exists a foo."
Definition: "...and here it is!"
There are interesting edge cases in C++ (some of them in C too). Consider
T t;
That can be a definition or a declaration, depending on what type T is:
typedef void T();
T t; // declaration of function "t"
struct X {
T t; // declaration of function "t".
};
typedef int T;
T t; // definition of object "t".
In C++, when using templates, there is another edge case.
template <typename T>
struct X {
static int member; // declaration
};
template<typename T>
int X<T>::member; // definition
template<>
int X<bool>::member; // declaration!
The last declaration was not a definition. It's the declaration of an explicit specialization of the static member of X<bool>. It tells the compiler: "If it comes to instantiating X<bool>::member, then don't instantiate the definition of the member from the primary template, but use the definition found elsewhere". To make it a definition, you have to supply an initializer
template<>
int X<bool>::member = 1; // definition, belongs into a .cpp file.
Declaration
Declarations tell the compiler that a
program element or name exists. A
declaration introduces one or more
names into a program. Declarations can
occur more than once in a program.
Therefore, classes, structures,
enumerated types, and other
user-defined types can be declared for
each compilation unit.
Definition
Definitions specify what code or data
the name describes. A name must be
declared before it can be used.
From the C99 standard, 6.7(5):
A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
for an object, causes storage to be reserved for that object;
for a function, includes the function body;
for an enumeration constant or typedef name, is the (only) declaration of the
identifier.
From the C++ standard, 3.1(2):
A declaration is a definition unless it declares a function without specifying the function's body, it contains the extern specifier or a linkage-specification and neither an initializer nor a function-body, it declares a static data member in a class declaration, it is a class name declaration, or it is a typedef declaration, a using-declaration, or a using-directive.
Then there are some examples.
So interestingly (or not, but I'm slightly surprised by it), typedef int myint; is a definition in C99, but only a declaration in C++.
From wiki.answers.com:
The term declaration means (in C) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user defined type or function in your program. No space is reserved in memory for any variable in case of declaration. However compiler knows how much space to reserve in case a variable of this type is created.
for example, following are all declarations:
extern int a;
struct _tagExample { int a; int b; };
int myFunc (int a, int b);
Definition on the other hand means that in additions to all the things that declaration does, space is also reserved in memory. You can say "DEFINITION = DECLARATION + SPACE RESERVATION" following are examples of definition:
int a;
int b = 0;
int myFunc (int a, int b) { return a + b; }
struct _tagExample example;
see Answers.
C++11 Update
Since I don't see an answer pertinent to C++11 here's one.
A declaration is a definition unless it declares a/n:
opaque enum - enum X : int;
template parameter - T in template<typename T> class MyArray;
parameter declaration - x and y in int add(int x, int y);
alias declaration - using IntVector = std::vector<int>;
static assert declaration - static_assert(sizeof(int) == 4, "Yikes!")
attribute declaration (implementation-defined)
empty declaration ;
Additional clauses inherited from C++03 by the above list:
function declaration - add in int add(int x, int y);
extern specifier containing declaration or a linkage specifier - extern int a; or extern "C" { ... };
static data member in a class - x in class C { static int x; };
class/struct declaration - struct Point;
typedef declaration - typedef int Int;
using declaration - using std::cout;
using directive - using namespace NS;
A template-declaration is a declaration. A template-declaration is also a definition if its declaration defines a function, a class, or a static data member.
Examples from the standard which differentiates between declaration and definition that I found helpful in understanding the nuances between them:
// except one all these are definitions
int a; // defines a
extern const int c = 1; // defines c
int f(int x) { return x + a; } // defines f and defines x
struct S { int a; int b; }; // defines S, S::a, and S::b
struct X { // defines X
int x; // defines non-static data member x
static int y; // DECLARES static data member y
X(): x(0) { } // defines a constructor of X
};
int X::y = 1; // defines X::y
enum { up , down }; // defines up and down
namespace N { int d; } // defines N and N::d
namespace N1 = N; // defines N1
X anX; // defines anX
// all these are declarations
extern int a; // declares a
extern const int c; // declares c
int f(int); // declares f
struct S; // declares S
typedef int Int; // declares Int
extern X anotherX; // declares anotherX
using N::d; // declares N::d
// specific to C++11 - these are not from the standard
enum X : int; // declares X with int as the underlying type
using IntVector = std::vector<int>; // declares IntVector as an alias to std::vector<int>
static_assert(X::y == 1, "Oops!"); // declares a static_assert which can render the program ill-formed or have no effect like an empty declaration, depending on the result of expr
template <class T> class C; // declares template class C
; // declares nothing
Definition :
extern int a; // Declaration
int a; // Definition
a = 10 // Initialization
int b = 10; // Definition & Initialization
Definition associates the variable with a type and allocates memory, whereas declaration just specifies the type but doesn't allocate memory. Declaration is more useful when you want to refer the variable before definition.
*Don't confuse definition with initialization. Both are different, initialization gives value to the variable. See the above example.
Following are some examples of definition.
int a;
float b;
double c;
Now function declaration :
int fun(int a,int b);
Note the semicolon at the end of function so it says it is only a declaration. Compiler knows that somewhere in the program that function will be defined with that prototype. Now if the compiler gets a function call something like this
int b=fun(x,y,z);
Compiler will throw an error saying that there is no such function. Because it doesn't has any prototype for that function.
Note the difference between two programs.
Program 1
#include <stdio.h>
void print(int a)
{
printf("%d",a);
}
main()
{
print(5);
}
In this, print function is declared and defined as well. Since function call is coming after the definition. Now see the next program.
Program 2
#include <stdio.h>
void print(int a); // In this case this is essential
main()
{
print(5);
}
void print(int a)
{
printf("%d",a);
}
It is essential because function call precedes definition so compiler must know whether there is any such function. So we declare the function which will inform the compiler.
Definition :
This part of defining a function is called Definition. It says what to do inside the function.
void print(int a)
{
printf("%d",a);
}
To understand the nouns, let's focus on the verbs first.
declare -
to announce officially; proclaim
define -
to show or describe (someone or something) clearly and completely
So, when you declare something, you just tell what it is.
// declaration
int sum(int, int);
This line declares a C function called sum that takes two arguments of type int and returns an int. However, you can't use it yet.
When you provide how it actually works, that's the definition of it.
// definition
int sum(int x, int y)
{
return x + y;
}
definition means actual function written & declaration means simple declare function
for e.g.
void myfunction(); //this is simple declaration
and
void myfunction()
{
some statement;
}
this is definition of function myfunction
Rule of thumb:
A declaration tells the compiler how to interpret the variable's data in memory. This is needed for every access.
A definition reserves the memory to make the variable existing. This has to happen exactly once before first access.
To understand the difference between declaration and definition we need to see the assembly code:
uint8_t ui8 = 5; | movb $0x5,-0x45(%rbp)
int i = 5; | movl $0x5,-0x3c(%rbp)
uint32_t ui32 = 5; | movl $0x5,-0x38(%rbp)
uint64_t ui64 = 5; | movq $0x5,-0x10(%rbp)
double doub = 5; | movsd 0x328(%rip),%xmm0 # 0x400a20
movsd %xmm0,-0x8(%rbp)
and this is only definition:
ui8 = 5; | movb $0x5,-0x45(%rbp)
i = 5; | movl $0x5,-0x3c(%rbp)
ui32 = 5; | movl $0x5,-0x38(%rbp)
ui64 = 5; | movq $0x5,-0x10(%rbp)
doub = 5; | movsd 0x328(%rip),%xmm0 # 0x400a20
movsd %xmm0,-0x8(%rbp)
As you can see nothing change.
Declaration is different from definition because it gives information used only by the compiler. For example uint8_t tell the compiler to use asm function movb.
See that:
uint def; | no instructions
printf("some stuff..."); | [...] callq 0x400450 <printf#plt>
def=5; | movb $0x5,-0x45(%rbp)
Declaration haven't an equivalent instruction because it is no something to be executed.
Furthermore declaration tells the compiler the scope of the variable.
We can say that declaration is an information used by the compiler to establish the correct use of the variable and for how long some memory belongs to certain variable.
Find similar answers here: Technical Interview Questions in C.
A declaration provides a name to the program; a definition provides a unique description of an entity (e.g. type, instance, and function) within the program. Declarations can be repeated in a given scope, it introduces a name in a given scope.
A declaration is a definition unless:
Declaration declares a function without specifying its body,
Declaration contains an extern specifier and no initializer or function body,
Declaration is the declaration of a static class data member without a class definition,
Declaration is a class name definition,
A definition is a declaration unless:
Definition defines a static class data member,
Definition defines a non-inline member function.
Declaration says "this thing exists somewhere"
int sampleFunc(); // function
extern int car; // variable
Definition says "this thing exists here; make memory for it"
int sampleFunc() {} // function
int car; // variable
Initialization is optional at the point of definition for objects, and says "here is the initial value for this thing":
int car = 0; // variable
Couldnt you state in the most general terms possible, that a declaration is an identifier in which no storage is allocated and a definition actually allocates storage from a declared identifier?
One interesting thought - a template cannot allocate storage until the class or function is linked with the type information. So is the template identifier a declaration or definition? It should be a declaration since no storage is allocated, and you are simply 'prototyping' the template class or function.
A declaration presents a symbol name to the compiler. A definition is a declaration that allocates space for the symbol.
int f(int x); // function declaration (I know f exists)
int f(int x) { return 2*x; } // declaration and definition
This is going to sound really cheesy, but it's the best way I've been able to keep the terms straight in my head:
Declaration: Picture Thomas Jefferson giving a speech... "I HEREBY DECLARE THAT THIS FOO EXISTS IN THIS SOURCE CODE!!!"
Definition: picture a dictionary, you are looking up Foo and what it actually means.
According to the GNU C library manual (http://www.gnu.org/software/libc/manual/html_node/Header-Files.html)
In C, a declaration merely provides information that a function or variable exists and gives its type. For a function declaration, information about the types of its arguments might be provided as well. The purpose of declarations is to allow the compiler to correctly process references to the declared variables and functions. A definition, on the other hand, actually allocates storage for a variable or says what a function does.
Adding definition and declaration examples from the C++ standard document(from the section 3.1 Declarations and definitions)
Definitions:
int a; // defines a
extern const int c = 1; // defines c
int f(int x) { return x+a; } // defines f and defines x
struct S { int a; int b; }; // defines S, S::a, and S::b
struct X { // defines X
int x; // defines non-static data member x
static int y; // DECLARES static data member y
X(): x(0) { } // defines a constructor of X
};
int X::y = 1; // defines X::y
enum { up, down }; // defines up and down
namespace N { int d; } // defines N and N::d
namespace N1 = N; // defines N1
X anX; // defines anX
Declarations:
extern int a; // declares a
extern const int c; // declares c
int f(int); // declares f
struct S; // declares S
typedef int Int; // declares Int
extern X anotherX; // declares anotherX
using N::d; // declares d
The concept of Declaration and Definition will form a pitfall when you are using the extern storage class because your definition will be in some other location and you are declaring the variable in your local code file (page). One difference between C and C++ is that in C you the declarations are done normally at the beginning of a function or code page. In C++ it's not like that. You can declare at a place of your choice.
My favorite example is "int Num = 5" here your variable is 1. defined as int 2. declared as Num and 3. instantiated with a value of five. We
Define the type of an object, which may be built-in or a class or struct.
Declare the name of an object, so anything with a name has been declared which includes Variables, Funtions, etc.
A class or struct allows you to change how objects will be defined when it is later used. For example
One may declare a heterogeneous variable or array which are not specifically defined.
Using an offset in C++ you may define an object which does not have a declared name.
When we learn programming these two terms are often confused because we often do both at the same time.
Stages of an executable generation:
(1) pre-processor -> (2) translator/compiler -> (3) linker
In stage 2 (translator/compiler), declaration statements in our code tell to the compiler that these things we are going to use in future and you can find definition later, meaning is :
translator make sure that : what is what ? means declaration
and (3) stage (linker) needs definition to bind the things
Linker make sure that : where is what ? means definition
There are some very clear definitions sprinkled throughout K&R (2nd edition); it helps to put them in one place and read them as one:
"Definition" refers to the place where the variable is created or assigned storage; "declaration" refers to the places where the nature of the variable is stated but no storage is allocated. [p. 33]
...
It is important to distinguish between the declaration of an external variable and its definition. A declaration announces the properties of a variable (primarily its type); a definition also causes storage to be set aside.
If the lines
int sp;
double val[MAXVAL]
appear outside of any function, they define the external variables sp and val, cause storage to be set aside, and also serve as the declaration for the rest of that source file.
On the other hand, the lines
extern int sp;
extern double val[];
declare for the rest of the source file that sp is an int and that val is a double array (whose size is determined elsewhere), but they do not create the variables or reserve storage for them.
There must be only one definition of an external variable among all the files that make up the source program. ... Array sizes must be specified with the definition, but are optional with an extern declaration. [pp. 80-81]
...
Declarations specify the interpretation given to each identifier; they do not necessarily reserve storage associated with the identifier. Declarations that reserve storage are called definitions. [p. 210]
The declaration is when a primitive or object reference variable or method is created without assigning value or object.
int a;
final int a;
The definition means assigning the value or object respectively
int a =10;
Initialization means allocating memory for a respective variable or object.
Declaration of a variable is for informing to the compiler the following information: name of the variable, type of value it holds and the initial value if any it takes. i.e., declaration gives details about the properties of a variable. Whereas, Definition of a variable says where the variable gets stored. i.e., memory for the variable is allocated during the definition of the variable.
Declaration means give name and type to a variable (in case of variable declaration), eg:
int i;
or give name,return type and parameter(s) type to a function without body(in case of function declaration), eg:
int max(int, int);
whereas definition means assign value to a variable (in case of variable definition), eg:
i = 20;
or provide/add body(functionality) to a function is called function definition, eg:
int max(int a, int b)
{
if(a>b) return a;
return b;
}
many time declaration and definition can be done together as:
int i=20;
and:
int max(int a, int b)
{
if(a>b) return a;
return b;
}
In above cases we define and declare variable i and function max().
This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 5 years ago.
Can you please someone explain me the flow of below problem,
#include <stdio.h>
int main(){
extern int a;
printf("%d\n",a);
return 0;
}
int a = 20;
and the output is 20. I am not sure where is the variable a getting defined and where is it getting declared?
The variable a is declared and defined as a global variable in the line:
int a = 20;
The extern line just tells the main() function scope that a is defined in another place.
In this case, the use of extern is not really necessary. You could just declare and define a before the main() function, and then main() would be familiar with it.
Usually, you would use extern when you want to use a variable or a function that was defined in another source file (and not just later in the same source file).
The C programming language has been designed to be one-pass, so that the compiler could process each line only once from top to bottom. So considering your program:
#include <stdio.h>
int main(){
extern int a;
printf("%d\n",a);
return 0;
}
int a = 20;
The identifier a is declared twice, and defined once.
Before the 4th line extern int a;, the compiler doesn't know anything about the identifier a. The declaration extern int a; has the block scope within the function main, and it declares the identifier a as an int and that its storage duration is static and linkage is external. So the compiler can write code that access a global identifier by name a as an int variable that could be defined in another module (external linkage). This is what the compiler does on line 5 when it is used in printf.
Finally at line 9, int a = 20; is another declaration and definition. This declares and defines a as a int with static storage duration, and external linkage.
If you'd put the int a = 20; before the main, the declaration extern int a; would be useless, because it doesn't add anything. I tend to put my main and other depending functions last in my source code, so that minimal amount of extra declarations are needed.
extern is syntactically a "storage class" keyword. But there is no such storage class. C has "static storage", "dynamic storage (malloc, etc) and "automatic storage" (local variables, usually represented using a stack).
If an identifier is declared extern inside a block scope, it means that the declaration refers to an external definition. If the entity being declared is an object, then it has static storage, simply because external objects have static storage. It can be a function too; functions aren't said to have storage.
In C, there is a concept called "linkage". Objects declared outside of any function at file scope, and functions, can have "external" or "internal" linkage.
If we have extern in a block scope, as you have in the example program, there can be a prior declaration of the same name at file scope, or in a nested scope, like this:
static int x;
/* ... */
{
extern int x;
}
Here, the inner x refers to the outer x, and, in spite of being "extern", it has internal linkage because of the "static".
In a nutshell, extern usually means "refer to the earlier declaration, and if there isn't one, declare this as an identifier with external linkage".
The word "external" refers to two separate concepts: the aforementioned "external linkage" and also to the meaning "outside of any function", as in "external declaration". Confusingly, "external declarations", like the static int x above, can have "internal linkage"!
In your program, things are correct because the block scope extern declaration of a and the later int a = 20, which are in separate scopes, happen to independently agree with each other.
The int a = 20; is an external declaration, which is also an external definition (because of the initializer). Since in that scope, no prior declaration of a is visible, it gets external linkage.
So, where is a defined? It is defined as an object with external linkage, in the entire translation unit as a whole. That translation unit is what defines a. a is declared in every place of the program where a declaration appears; and its definition is also a declaration. It is declared in main and also in the last line of the translation unit's source code.
A "declaration" is syntax which makes a name known in some scope. It's a concept that is active during the translation of a program. A "definition" is the fact that some object or function is provided in some translation unit. Translated units still provide definitions, but need not retain information about declarations. (Which is why when we make libraries, we provide header files with declarations in them!)
From the point of view of your main function, that function doesn't "care" where a is defined. It has declared a in such a way that if a is used, then an external definition of a, with external linkage, must exist. That definition could come from anywhere: it could be in the same translation unit, or in another translation unit.
When ever you declare a variable as extern then It means that Variable is declared as global and you cannot initialize the variable there.Because no memory is allocated for that variable It is just declared as a Variable
you can define it some where in your code.
Let us take an example ..consider the code
int main()
{
extern int i;
i=10;
printf("%d",sizeof(i));
}
here you get an error that int 'i' is not defined
therefore you need to write it as:
int main()
{
extern int i;
int i=10;
printf("%d",sizeof(i));
}
In case of your code:
This is declaration
extern int a;
This is definition:
int a = 20;
The storage class extern specifies storage duration and linkage of the object the identifier refers to:
The storage duration is set to static, which means the variable is alive for the whole time the program runs. As you declare that variable inside a function scope, this matters in your example, because in function scope, the default storage duration would be automatic.
The linkage is set to external, this just means different translation units of the same program can share the object. It has the side effect that a definition in another (file) scope is acceptable, as shown in your example. This is a logical consequence of the shared nature, you would typically declare a variable with external linkage in all translation units using it, but define it only in one.
Consider the code:
int main(void)
{
int a;
}
As far as I know, int a; is a definition, as it causes storage to be reserved. Citing the C standard (N1570 Committee Draft — April 12, 2011):
6.7/5 Semantics
A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
— for an object, causes storage to be reserved for that object;
...
Here comes the question: the compiler may optimize away the storage, since we are not using the variable. Is then int a; a declaration then? And what if we do a printf("%p", &a) in main(void) - certainly now the compiler has to allocate storage, so is the concept of declaration/definition dependent on whether you later use the identifier or not?
The text you quoted from 6.7/5 is actually meant to be interpreted the other way around than what you have done: the text is saying that definitions cause storage to be allocated.
The text which specifies that int a; is a definition is elsewhere.
C is defined in terms of an abstract machine. There is storage allocated in the abstract machine. Whether or not any memory is allocated on your PC is unrelated.
Is then int a; a declaration then?
Yes.
In fact, every definition is also a declaration. A variable can have only one definition, but could have multiple declarations.
int a;
This is a definition
There is a memory allocated for variable a
extern int a;
This is a declaration.
Memory is not allocated because it is not defined.
Once a variable is defined you can use the address of it which is totally legal.
A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:
extern int bar;
extern int g(int, int);
A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:
int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
I am new to C and I experience some confusion between the declaration and definition of a variable. Another thing I would like to know is if the following is true:
"Declaration appears many times and definition comes once."
Also:
int x;
Is this a declaration only? Since memory is allocated for x then why isn't this a definition instead of a declaration?
Simply writing int x; at either global scope or local scope, is both a declaration and definition. Typically, the declaration tells the compiler "This variable will exist, at some point, under this name, so you can use it." The definition tells the compiler to actually arrange for the variable to be created - obviously this can only happen once.
Typically the way you'll use this is by putting in a header file:
// Foo.h
#ifndef FOO_H
#define FOO_H // make sure structs aren't redefined
extern int bar; // Declare a variable bar
#endif
And in a single source file
#include "foo.h"
int bar; // Define bar
If you were to define bar in multiple files, you would get an error; you can't create the variable twice. But you do have to tell the compiler about it in every source file you use bar in. Hence the extern declaration.
The precise semantics are defined in §6.9.2 of the C standard and can be summarized as follows:
When a variable is declared at file scope with an initializer, it is an external definition. (§6.9.2/1)
When a variable is declared at file scope without an initializer, and without a storage-class specifier or with the static storage-class specifier, it is a tentative definition. If the translation unit (file) has one or more tentative definitions and no external definition, the compiler automatically adds a true file scope declaration at the end of the translation unit, with a zero initializer. (§6.9.2/2)
What this means is that, strictly speaking, int x; is not a definition; but it automatically creates a definition if and only if there is no other definition with an initializer, and no static definition (this third case is undefined behavior due to linkage disagreement per §6.2.2/7)
Note that extern int x; is not an external definition. It is a declaration with an extern storage class specifier. As such, extern int x; alone does not cause a definition to be created, but if you have both:
extern int x;
int x;
Then you will end up having a definition created at some point in the file.
It is also, technically speaking, legal to do this:
extern int x;
int x;
int x = 42;
In this case, the int x; in the middle is superfluous and has no effect. That said, this is poor form, as it's confusing in this case where the actual definition is.
This isn't something you see too much in C, but it works like this:
In a header file, you can have a line like this:
extern int x; //declaration
Because of the extern modifier, this tells the compiler that there is an int named x somewhere. The compiler doesn't allocate space for it - it just adds int x to the list of variables you can use. It'll only allocate space for x when it sees a line like this:
int x; //definition
You can see that because only the int x; line changes your executable, you can have as many extern int x; lines as you feel like. As long as there's only int x; line, everything will work like you want it to - having multiple declarations doesn't change a thing.
A better example comes from C++ (sorry if this is a C-only question - this applies to structs as well, but I don't know the syntax off the top of my head):
class Pineapple; //declaration
Pineapple* ptr; //this works
Pineapple pine; //this DOES NOT work
This declaration tells the compiler that there's a class called "Pineapple". It doesn't tell us anything about the class (how big it is, what its members are). We can use pointers to Pineapples now, but we can't yet have instances - we don't know what makes up a Pineapple, so we don't know how much space an instance takes up.
class Pineapple
{
public:
int ounces;
char* name;
}; //definition
Pineapple* ptr; //still works
Pineapple pine; //this works now too!
//we can even get at member variables, 'cause we know what they are now:
pine.ounces = 17;
After a definition, we know everything about the class, so we can have instances, too. And like the C example, you can have multiple declarations, but only one definition.
Hope this helps!