declaration Vs Definition

This is a discussion on declaration Vs Definition within the C Programming forums, part of the General Programming Boards category; I did a search on google but didn't find satisfactory answer. please tell clarify it to me. what is the ...

  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,185
    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,261
    "extern", in this case, essentially means defined elsewhere (i.e. externally) .
    Right 98% of the time, and don't care about the other 3%.

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

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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%.

  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
    21,636
    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 09:00 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 04:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21