Thread: declaration Vs Definition

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by tabstop View Post
    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.
    in your example f is a definition but q is a tentative definition. it's only a definition if there isn't another definition for q somewhere else. if there is another definition then q is a declaration.
    Code:
    int q; // definition
    int q; // declaration/tentative definition
    the comments might not be right. i'm not sure if the order of tenativeness is defined so it could be the other way around.

  2. #17
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Meldreth View Post
    Code:
    int q; // definition
    int q; // declaration/tentative definition
    That won't compile!
    Code:
    error: redeclaration of 'q' with no linkage
    A declaration tells the compiler "watch out, there's this symbol you should know about. I'll tell you more later".
    A definition tells the compiler "so this is everything you need to know about this symbol"
    You don't need a declaration, but you do need to have a definition (depending on where you need to use the symbol).

    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

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by QuantumPete
    That won't compile!
    Actually, Prelude once showed me that it can:
    Code:
    int q;
    int q;
    
    int main(void)
    {
        return 0;
    }
    Unfortunately, I am not conversant enough in this area to tell you exactly what the C standard says, much less why C has such a feature.
    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

  4. #19
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by QuantumPete View Post
    That won't compile!
    Code:
    error: redeclaration of 'q' with no linkage
    you're compiling as c++. c++ doesn't allow tentative definitions.

  5. #20
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Meldreth View Post
    you're compiling as c++. c++ doesn't allow tentative definitions.
    The code is in a file called test.c and I'm running gcc test.c (gcc version 3.4.6)
    With g++ I get:
    Code:
    error: redeclaration of `int q'
    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

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, back from a shower, and now I get what the C standard says
    Quote Originally Posted by C Standard, 1999 edition, Section 6.2.2, Paragraph 2
    In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.
    Quote Originally Posted by C Standard, 1999 edition, Section 6.2.2, Paragraph 6
    The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier extern.
    So, in my adaptation of Prelude's example, q has internal linkage, so the two declarations denote the same object, hence there is only one definition of q, and that is perfectly fine. If we make q into a local variable, it has no linkage, which means that each declaration of q denotes a unique entity, thus the error.

    I still do not understand why it is the way that it is, but at least I can definitely say that it really is part of the core language and not a compiler extension.
    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

  7. #22
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by QuantumPete View Post
    The code is in a file called test.c and I'm running gcc test.c (gcc version 3.4.6)
    With g++ I get:
    Code:
    error: redeclaration of `int q'
    QuantumPete
    then gcc is wrong, or your code is wrong. laserlight posted a program that uses tentative definitions. do you get the same error with that one?

  8. #23
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Meldreth View Post
    then gcc is wrong
    I find that hard to believe!
    But yeah, laserlight's code compiles fine. It seems to work only for globals.

    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

  9. #24
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    It seems to work only for globals.
    your code was wrong.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by QuantumPete
    It seems to work only for globals.
    To be precise, it only works for objects with linkage. You probably did this, in which case q has no linkage, thus gcc is correct to report an error:
    Code:
    int main(void)
    {
        int q;
        int q;
        return 0;
    }
    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

  11. #26
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by laserlight View Post
    To be precise, it only works for objects with linkage. You probably did this, in which case q has no linkage, thus gcc is correct to report an error:
    Code:
    int main(void)
    {
        int q;
        int q;
        return 0;
    }
    Yes, that's precisely what I did. I'm intrigued now though. Why would internal linkage allow multiple declarations of the same symbol? What's the use? I've tried googling, but it doesn't seem to be a widely discussed issue.

    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

  12. #27
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by matsp View Post
    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.
    Yeah, indeed. I wrote quickly, and mixed up whether A or B is the synonym. The basic message, however, is correct.
    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.

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