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 ?
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.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.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
Wouldn't the difference be more like:
Code:// Definition: typedef struct Pair { int first; int second; }; // Declaration: Pair a;
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
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.Originally Posted by cpjust
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is.Originally Posted by King Mir
However, this is one thing that I am still uncertain of. Consider: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; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
She stoops to conquer
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.
Last edited by itCbitC; 12-17-2009 at 11:15 AM.
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
Last edited by King Mir; 12-17-2009 at 11:21 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.