Declarations and definitions in C

This is a discussion on Declarations and definitions in C within the C Programming forums, part of the General Programming Boards category; Guys what is the difference between declarations and definitions of varibles,functions, structures? I understand Code: int c; int d; are ...

  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
    Posts
    23,033
    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
    Posts
    23,033
    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
    Posts
    23,033
    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
    22,312
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    32,855
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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, 02: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, 10:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21