Thread: Variable declaration Vs definition

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Galle, Sri Lanka
    Posts
    3

    Variable declaration Vs definition

    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 ?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's more language than anything.

    Pretty much what you said is fine.

    Quote Originally Posted by callkalpa
    Then what is "int a = 10;" called ?
    Declaration of "a" of type int, initialised to 10.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    All definitions are declarations. A declaration is more or less when you tell the compiler what a particular identifier is, so:
    Code:
    int i;
    void f(void);
    Both are declarations. A definition is a declaration that either creates the variable or includes a function body. So:
    Code:
    int i; /* in a function */
    void f(void) { return; }
    These are definitions. You can create a variable declaration that's not a definition by doing something like:
    Code:
    extern int i;
    This says that “i” is declared somewhere else; so you're telling the compiler what “i” is, but not to reserve space for it.

    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.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by callkalpa View Post
    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 ?
    int a;----------->Definition
    a=10------------->Assignment
    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

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    Wouldn't the difference be more like:
    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.
    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. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    I'm not sure if that would technically be called a declaration in the standard
    It is.

    Quote Originally Posted by King Mir
    but it does behave similar to declarations of other things, particularly in that it can apear more than once in a program.
    However, this is one thing that I am still uncertain of. Consider:
    Code:
    int n;
    int n;
    
    int main(void)
    {
        n = 1;
        return 0;
    }
    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?
    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
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    However, this is one thing that I am still uncertain of.
    She stoops to conquer
    Quote Originally Posted by laserlight View Post
    Consider:
    Code:
    int n;
    int n;
    
    int main(void)
    {
        n = 1;
        return 0;
    }
    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?
    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.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by laserlight View Post
    However, this is one thing that I am still uncertain of. Consider:
    Code:
    int n;
    int n;
    
    int main(void)
    {
        n = 1;
        return 0;
    }
    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?
    Those are tentative definitions. They don't reserve space as such, but tell the compiler that unless a “real” definition (one with an initializer) is seen later on, it should act as though it saw a definition with an initializer of zero.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. declaration or definition
    By BEN10 in forum C Programming
    Replies: 5
    Last Post: 07-19-2008, 07:50 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Declaration vrs Definition
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2004, 07:13 PM