I did a search on google but didn't find satisfactory answer.
please tell clarify it to me.
what is the difference between variable declaration and definition?
I did a search on google but didn't find satisfactory answer.
please tell clarify it to me.
what is the difference between variable declaration and definition?
A declaration says "there is a variable, somewhere, with this name". So
is a declaration.Code:extern int x;
A definition does that, plus sets aside the requisite memory location for the variable to live in. So
are definitions.Code:int q; float f = 6.5f;
thank you.
would you please explain what does it mean by extern?
I could have searched for this but I think you would rather clarify it to me in a simple language and example.
"extern", in this case, essentially means defined elsewhere (i.e. externally) .
That distinction is used only in the context of external variables and functions. A declaration tells the type of the variable while its definition reserves storage for it.
Hence my usage of the words "in this case".
That's not true. Variable definitions require the type of the variable to be specified as well.
The standard is a little more specific, but a declaration specifies the interpretation, by the compiler, of a set of identifiers. A definition is a particular form of declaration. If the identifier is the name of an object, a definition causes storage to be reserved for that object (this is a variable definition). If the identifier is a function, the definition includes the function body. If the identifier is an enumeration constant or a typedef name, the definition is the only declaration allowed.
I was addressing the o/p when I posted that; and yes the definition declares the type and allocates storage for the variable, atleast that's what was implied.
Can you please give reason for why for typedef definition is the only declaration allowed?
Isn't typedef just for the compiler to replace with original declaration (sort of macro type functionality) ? or typedef's have some other functionality too?
I found the following explanation on some online C book.
Although typedef is thought of as being a storage class, it isn't really. It allows you to introduce synonyms for types which could have been declared some other way. The new name becomes equivalent to the type that you wanted
There is no replacement since the typedef defines the name as a synonym for another type name. Unlike macros, typedefs obey the rules of scope.Originally Posted by vkaushal21
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Having a declaration that is not a definition only makes sense if whatever is declared can be used in some context without knowledge of the definition.
"typedef A B;" makes A a synonym (an alternative name) of B. No usage of A is possible without knowing exactly that it is a synonym of B.
To understand the answer to that, you need to realise there are various phases of compilation for any source file. The phases are specified in the standard and the net effect is as if they occur sequentially, with output from one phase being fed as input to the next. [Particular tool chains may implement things differently, but the net effect is required to be as if the phases were executed sequentially].
The preprocessor is one of the early phases, and the compiler is a later phase.
Macros are executed as text substitutions by the preprocessor, before the code ever reaches the compiler. A typedef is an instruction that is ignored (or allowed to pass through unmolested) by the preprocessor and acted on by the compiler.
That sequence is the reason that macros disobey scope but typedefs do (scoping rules are exercised by the compiler, not the preprocessor).
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
That's true if you're using typedef to assign a new name to an already existing type with an already existing name, as in "typedef int foo". But that's not the purpose of the typedef statement, because a macro would do as well.Yes, I expressed it in a not so concrete way because in practice, BOTH sides are, after the typedef, synonyms - they mean the same thing and can be used in the same way.
The purpose of typedef is to introduce identifiers for types which don't already have a name.
Consider the following code snippet:
This won't compile. gcc tells us that it's an "assignment from incompatible pointer type". No macro can help us here, we need typedef:Code:int main() { struct { int x; } *a; struct { int x; } b; a = &b; }
Now it compiles just fine. Note that we didn't merely introduce a synonym for "struct { int x; }". Instead, we defined a new type called "foo" which has the same range and representation as "struct {int x;}". But these are not just different names for the same thing, these are different types, the most obvious difference being that "foo" has a name while the other hasn't. Also, they cannot be used interchangeably.Code:typedef struct {int x; } foo; int main() { foo *a; foo b; a = &b; }
Greets,
Philip
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
Macros do not obey the rules of scope.Originally Posted by Snafuist
The general purpose of typedef is to provide a synonym for another type. Consequently, you would right to say that that is a purpose of typedef.Originally Posted by Snafuist
No, the typedef makes foo a synonym for the anonymous type introduced by the struct definition. By itself, a typedef does not create a new type, at least according to the C standard.Originally Posted by Snafuist
EDIT:
I'd say that the reason why your first example gave the "assignment from incompatible pointer type" error is that the types of *a and b are different. If they were the same, then it would break the one definition rule since the same struct type would have been defined twice.
Last edited by laserlight; 02-27-2009 at 10:00 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)