Thread: Declaration vrs Definition

  1. #1
    Registered User
    Join Date
    Jul 2003

    Declaration vrs Definition

    I just learned somthing new.
    While I thought I knew what definiton and declaration meant I was wrong about definition. I assumed that defining a variable meant initializing it with a value but
    char ch; is both a declaration and definition.
    a definition associates a type with a name
    in the above case this is a definition
    for the definition there is an entity for the name to which it refers
    for ch the entity is the appropriate amount of memory to be used as a variable

    also any declaration that specifies a value is a definition (from this I assumed that if it doesn't it is not a definition)

    (Strostrup, "C++ Programming Language")

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    A declaration simply states that a name exists and what its type is. A definition sets aside memory, connects that memory to the name, and possibly even initializes the memory with a value. A definition is a declaration, but a declaration might not be a definition. You can avoid any sticky rules by following two simple guidelines:

    * An object declaration preceded by extern is not a definition.
    * A function without a body is not a definition.
    * Everything else is a definition and can only appear once.
    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