Thread: declaration Vs Definition

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    declaration Vs 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?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A declaration says "there is a variable, somewhere, with this name". So
    Code:
    extern int x;
    is a declaration.

    A definition does that, plus sets aside the requisite memory location for the variable to live in. So
    Code:
    int q;
    float f = 6.5f;
    are definitions.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    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.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    "extern", in this case, essentially means defined elsewhere (i.e. externally) .
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by itCbitC View Post
    That distinction is used only in the context of external variables and functions.
    Hence my usage of the words "in this case".
    Quote Originally Posted by itCbitC View Post
    A declaration tells the type of the variable while its definition reserves storage for it.
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Quote Originally Posted by grumpy View Post
    If the identifier is an enumeration constant or a typedef name, the definition is the only declaration allowed.
    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

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vkaushal21
    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?
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by vkaushal21 View Post
    Can you please give reason for why for typedef definition is the only declaration allowed?
    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.

    Quote Originally Posted by vkaushal21 View Post
    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?
    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).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by grumpy View Post
    "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.
    I always thought that the typedef works the other way around: B is a synonym of A - meaning that B didn't exist before as a name, and is now the equivalent of A.

    --
    Mats
    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.

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    I always thought that the typedef works the other way around: B is a synonym of A
    That's correct, I often write
    Code:
    typedef unsigned long long ulong
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    That's correct, I often write
    Code:
    typedef unsigned long long ulong
    QuantumPete
    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. But it CREATES the new name from B in the original example (ulong in your example).

    --
    Mats
    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.

  14. #14
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    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.
    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.

    The purpose of typedef is to introduce identifiers for types which don't already have a name.

    Consider the following code snippet:

    Code:
    int main()
    {
            struct { int x; } *a;
            struct { int x; } b;
    
            a = &b;
    }
    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:
    typedef struct {int x; } foo;
    
    int main()
    {
            foo *a;
            foo 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.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Snafuist
    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.
    Macros do not obey the rules of scope.

    Quote Originally Posted by Snafuist
    The purpose of typedef is to introduce identifiers for types which don't already have a name.
    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.

    Quote Originally Posted by Snafuist
    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.
    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.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM