declaration or definition

This is a discussion on declaration or definition within the C Programming forums, part of the General Programming Boards category; since i'm studying C,i'm a bit confused regarding declartion and definition of a variable.for eg i don't understand whether Code: ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    declaration or definition

    since i'm studying C,i'm a bit confused regarding declartion and definition of a variable.for eg i don't understand whether
    Code:
    int i;
    is a declaration or a definition.also if i do
    Code:
    int i;
    i=10;
    here i=10 is a declaration or definition.
    plz help me regarding this issue.thanks.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    I believe
    Code:
    int i;
    is a decleration, while

    Code:
    i=10;
    is an assignment.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
    =
    is an assignment operator.

    so,
    Code:
    expression = expression
    is an assignment.

    E.g.
    Code:
    a = 10;
    *(b + a) = 20;
    c[2] = 30;

    but, whenever we found
    Code:
    data-type [modifier(s)] identifier [modifier(s)]
    is a declaration.

    E.g.
    Code:
    char *str;
    short n;
    int array[10];

    In C, we could combine them of course.
    Code:
    data-type [modifier(s)] identifier [modifier(s)] = expression
    E.g.
    Code:
    char *str = "Hello BEN10";
    short n = 123;
    int array[10] = {0, 1, 2, 3, 4, 5, 6, 8, 9};

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    one thing that i know is that while defining a variable space gets reserved for it in the memory whereas while declaring no such thing happens.
    if
    Code:
    int i;
    is a declaration then when i do
    Code:
    int i;
    printf("%u",&i);
    why it gives the address of the variable i.plz solve my query and if i'm wrong anywhere
    correct me.thanks

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Memory is reserved when you declare a variable, although the details of how the memory gets reserved varies. In your code example, you're printing the address..... It's printing the address because you're telling it to.

  6. #6
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    All definitions are declarations. A definition is just a special form of declaration that happens to reserve storage, or in the case of a function, provide the body.

    If you do:
    Code:
    int i;
    This is a definition (sometimes it's actually a tentative definition, but we can pretend it's just a normal definition).

    If you do:
    Code:
    extern int i;
    This is not a definition. No space has been reserved for i, because you're telling the compiler it's defined elsewhere.

    If you do:
    Code:
    void f(void) { }
    This is a definition, because you're including a function body.

    If you do:
    Code:
    void f(void);
    This is not a definition because, of course, there is no function body.

    All of the above are (or include) declarations, but not all are definitions.

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, 02:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11: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, 07:39 AM

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