Thread: Declarations and definitions in C

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    Declarations and definitions in C

    Guys what is the difference between declarations and definitions of varibles,functions, structures?
    I understand
    Code:
    int c;
    int d;
    are examples of declarations and int c=4; is an initilisation but how does variable definition differ?

    Same with structures
    Code:
    struct autopart
    {
    char id;
    float price;
    }
    //is the structure declaration.What is the definition of a structure.

    for functions
    Code:
    int sum2digits(int a,int b);  //the prototype
    lets say...
    Code:
    int sum2digits(int a, int b)  //what is this line called?
    {
    return a+b;
    }
    what are the definitions and declarations.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The last one if a function definition. The function before is a declaration - telling the compiler it exists.
    I'm not 100% if to call declaring or defining variables, but I'd go for declaring them. You can also initialize them as you say.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    edit: got them mixed up.

    Definitions are when you do this: struct X { int x,y; }; and declarations are when you make instances, like this: struct X x; int y; char a; and an initialization like you wrote is also a declaration.

    Basically, declarations make instances for you to work with, and definitions make types.

    Code:
    int sum2digits(int a, int b) //what is this line called?
    {
    return a+b;
    }
    function signature, I guess. It doesn't really have a name since it isn't expressed in individuality except for a prototype.
    Last edited by robwhit; 12-17-2007 at 11:27 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is a function definition; as contrary to a function declaration which is only the prototype with a ; at the end and no body. Exists to tell the compiler a function exists (and some IDEs also extract information from prototypes as to what is expect from the function, like what arguments it takes - Visual Studio is one!).

    Code:
    struct X { int x,y; };
    That is without doubt a declaration. It's a blueprint of the struct - it does not implement its definition.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    drat... it's so confusing...

    edit: I couldn't find the word definition anywhere in my C99 draft. interesting.
    Last edited by robwhit; 12-17-2007 at 11:11 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suppose you could say that you declare what a function or a data type (struct) is, but you must define its behavior (code/implementation).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Although it is for C++, Stroustrup's glossary could come in handy:

    declaration - an introduction of a name into a scope. The type of the name must be specified. If the declaration also specifies the entity to which the name refers, the declaration is also a definition.

    definition - a declaration that specifies the entity to which the declared name refers.
    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

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by K&R-II, A8, p210
    Declarations specify the interpretation given to each identifier; they do not necessarily reserve storage associated with the identifier. Declarations that reserve storage are called definitions.
    So basically, if it takes up memory space, then it's a definition.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So basically, a prototype or a struct is not a definition; it's a declaration.
    Variables that can reserve space for a said type, is a definition, then.
    Implementation of function, refering to a prototype or a declaration, would also be a definition due to code, which would also take space in memory.

    That's what I gather.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 01:59 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 11:45 PM
  4. help on declarations
    By robasc in forum C Programming
    Replies: 9
    Last Post: 03-05-2005, 01:50 PM
  5. help writing function definitions
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2003, 09:44 PM