Thread: Declaration vs definition

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Question Declaration vs definition

    Could someone tell me the difference between a declaration and definition of a variable????

    Thx in advance

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Well.. I dunno about a "variable", but here's the diff for functions (applies to other things, of course)

    Code:
    void MyFunc (void); // declaration
    
    int main()
    {
       MyFunc();
       return(0);
    }
    
    void MyFunc (void) // definition
    {
       cout << "MyFunc\n";
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    29
    I think (not 100% sure) that when something (a variable, struct)is defined memory is not allocated. When it is declared memory is allocated.
    Colin

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I agree with Colin:

    You "define" a variable type. Here is a sample user defined variable type:


    struct Sample
    {
    int num;
    char name[80];
    };


    You "declare" an instance of some type, said instance often called an object. Here is one way (there are others) to declare an instance of a type, in this case an instance of the struct type called Sample.

    Sample sample1;

    Memory is set aside when an object is declared, not when a type is defined. The definition tells the compiler how much memory to allocate for a given type, but doesn't the compiler to actually allocate any memory yet.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Here's how I look at it:

    int x; // declare a variable
    x = 345; // define a variable

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    int x; //declare variable

    x = 35;//assign a value to variable

    int y = 24;//initialize a variable with a value, looks like an assignment, but it's not

    the type called int is defined in iostream.h someplace, I think.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Could someone tell me the difference between a declaration and definition of a variable????
    A declaration gives the compiler information about the type of an identifier. A definition is a declaration, but a declaration isn't always a definition. A definition means that you are allocating storage for a variable and optionally initializing it with a value. Consider a variable with file scope:

    /* The definition can only occur once in the program
    */
    int value = 32;

    /* The declaration could be made anywhere in the program.
    ** Even in the same file that variable was defined without effecting
    ** the definition.
    */
    extern int value;

    -Prelude
    My best code is written with the delete key.

  8. #8
    Think of a declaration as CREATING and definition as USING.

    //declare your variables
    int i;
    int j;
    long k;


    //define your variables.
    i=36;
    j=52;
    k=266;

    i=j;

    Header files hold the declarations of functions, variables, and classes. Those functions, variables, and classes can be defined in a separate CPP file.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Think of a declaration as CREATING and definition as USING.
    No, don't. It's attempts at abstraction such as this that cause confusion. "Well, it's like this, so think of it like this even though it's completely different"

    Note that I said a definition can only occur *once* in a program. You can allocate the memory for a variable *once* for that identifier. Using, defining, and declaring are all different. The differences may seem subtle and useless, but they are very important.

    -Prelude
    My best code is written with the delete key.

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