In C what is meant by variable declaration and variable definition?
int a; -----> declaration
a = 10; -------> definition
Is this correct? Or it is other way around?
Then what is "int a = 10;" called ?
Printable View
In C what is meant by variable declaration and variable definition?
int a; -----> declaration
a = 10; -------> definition
Is this correct? Or it is other way around?
Then what is "int a = 10;" called ?
It's more language than anything.
Pretty much what you said is fine.
Declaration of "a" of type int, initialised to 10.Quote:
Originally Posted by callkalpa
All definitions are declarations. A declaration is more or less when you tell the compiler what a particular identifier is, so:
Both are declarations. A definition is a declaration that either creates the variable or includes a function body. So:Code:int i;
void f(void);
These are definitions. You can create a variable declaration that's not a definition by doing something like:Code:int i; /* in a function */
void f(void) { return; }
This says that “i” is declared somewhere else; so you're telling the compiler what “i” is, but not to reserve space for it.Code:extern int i;
The whole “in a function” comment above is because if you do "int i" globally, it's what's called a “tentative definition”, which is more or less a definition, eventually. Don't worry about that one too much.
In short: a definition is a declaration that either reserves space for a variable or includes a function body.
Wouldn't the difference be more like:
Code:// Definition:
typedef struct Pair
{
int first;
int second;
};
// Declaration:
Pair a;
Once you correct the typedef, they would both be definitions. The former is a struct definition, the latter is a variable definition. Of course, since they are both definitions, they are both declarations.Quote:
Originally Posted by cpjust
Main difference between a declaration and a definition is that a declaration states the properties of the variable namely its type while a definition allocates storage for it also.
For variables, the only way to declare a variable without defining it is to declare it with the extern keyword and not specify an initial value. I'm not sure if that would technically be called a declaration in the standard, but it does behave similar to declarations of other things, particularly in that it can apear more than once in a program.
It is.Quote:
Originally Posted by King Mir
However, this is one thing that I am still uncertain of. Consider:Quote:
Originally Posted by King Mir
Now, if I understand C99 correctly, n has external linkage. Consequently, having both declarations there is perfectly fine. However, since we can use n, storage must have been reserved for it. So, which declaration of n is its definition?Code:int n;
int n;
int main(void)
{
n = 1;
return 0;
}
She stoops to conquer :cool:
They are both definitions because storage is allocated for them, however it is upto the compiler whether it replaces the second definition of n with the first one or does it simply add another entry to the symbol table tho' I very strongly lean towards the former, to eliminate ambiguity. An extern statement is a declaration, all others being definition since storage is allocated for the rest. I guess a function prototype is the best example of a declaration, it states the function attributes ie how many parameters it takes and their type and the return type.
The first has to be the one that reserves storage. Imagine if there was another function that used n between the two declarations. C is made so that you can implement a single pass compiler. Therefore that inserted function would have to treat the first declaration as a definition, since it does not see the second. When main comes around, it has to use the same storage space as the first function, so the first declaration is used.
Case point