Thread: Is int i; is a declaration or definition?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    Is int i; is a declaration or definition?

    int i;
    printf("%d",i);

    If you execute above code, you will get some junk value as output. It means memory was allocated to i. Hence it is definition. But, many book refers its declaration as the value is explicitly not assigned to i. So, is this declaration or definition? Or depending upon compiler implementation it is declaration if memory is not allocated.

  2. #2
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    That is a definition, and every definition is also a declaration.

    In order to declare but not define a variable, you do this:

    Code:
    extern int x;

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    extern int i;
    i=50;

    it wont work You have to initialise the variable in differnt scope then

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No it's a declaration, your declaring 'i' as an integer. But your not defining it a value.

    A declaration and a definition would be:
    Code:
    int i = 10;
    i has been declared as an integer, and defined as 10.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Decralation and Defination are terms, that we use, not the compiler.
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by zacs7 View Post
    No it's a declaration, your declaring 'i' as an integer. But your not defining it a value.

    A declaration and a definition would be:
    Code:
    int i = 10;
    i has been declared as an integer, and defined as 10.
    You're confusing definition and initialization.

    All a declaration does is introduce a name into the compiler's symbol table. It doesn't generate any code when compiled.

    Code:
    extern int i;
    This informs the compiler that i is an int. It allocates no space for i, and it now falls upon the linker to actually locate the storage space that is i. It usually exists in a separate file, or more accurately, a separate translation unit.

    Code:
    void funk(int x);
    This is a function declaration. Once again, this simply informs the compiler that funk is void function that takes an int. The compiler does not know where the instructions that make up funk() actually are, but it knows enough to check the syntax.

    Code:
    class Thing;
    This is a class declaration. The compiler now knows the Thing is a class, which allows you to define Thing pointers and references, despite the compiler having never seen the definition of Thing.

    Code:
    extern int x = 0;
    This is perfectly legal and will compile, but it will not link(important distinction) unless x is defined somewhere. Most compilers have an option to compile, but not link a file. Experiment with this and it may become more clear.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So, is this declaration or definition?
    It's both. Your book (assuming it's a good one) probably meant the way that i was declared and not the point at which it was defined. There's a subtle difference there if you look at silly--but legal--code like this:
    Code:
    #include <stdio.h>
    
    int i;
    int i;
    int i;
    int i;
    int i;
    int i;
    
    int main ( void )
    {
      i = 10;
    
      printf ( "%d\n", i );
    
      return 0;
    }
    My best code is written with the delete key.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by AverageSoftware
    You're confusing definition and initialization.
    No, think of it mathematically. If you 'initialize' a variable with a value your still defining it regardless if it's at 'declaration' time or not.
    Code:
    int i;
    i = 10;
    * i is declared as an integer
    * i is defined as 10

    IS the same as
    Code:
    int i = 10;
    * i is declared as an integer
    * i is defined as 10

    yes your initializing it with a value, but your still technically 'defining' i...

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, think of it mathematically. If you 'initialize' a variable with a value your still defining it regardless if it's at 'declaration' time or not.
    AverageSoftware's point is that to initialise is to define, but the lack of an initialisation does not mean the lack of a definition. You stated: "No it's a declaration, your declaring 'i' as an integer. But your not defining it a value."

    In other words, you claim that:
    int i;
    is not a definition, which is false. It may be true in your "mathematical" terms, but then what's the equivalent of printf("&#37;d",i) in mathematics? This is C programming, not mathematics. Here is what C99 (section 6.7) has to say about the topic:

    A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:
    — for an object, causes storage to be reserved for that object;
    — for a function, includes the function body;
    — for an enumeration constant or typedef name, is the (only) declaration of the identifier.
    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

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Although, you don't assign 'i' a value (but it will have one from the last value in at that address) it is still your still technically 'defining' 'i' by declaring it...?

    While it has a value, I wouldn't say you've defined it... I don't mean to be a pain, but I'm trying to understand as to what you mean

    But looking back on the OPs question I see where you're coming from
    Last edited by zacs7; 05-30-2007 at 01:30 AM.

  11. #11
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by zacs7 View Post
    No, think of it mathematically. If you 'initialize' a variable with a value your still defining it regardless if it's at 'declaration' time or not.
    I'm thinking of it in terms of the C and C++ standards, where "declare" and "define" (and "initialize") have very specific meanings. It is likely those meanings that the original poster's book was referring to.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by AverageSoftware View Post
    I'm thinking of it in terms of the C and C++ standards, where "declare" and "define" (and "initialize") have very specific meanings. It is likely those meanings that the original poster's book was referring to.
    Yeah, Sorry if I was an ass, I was thinking in mathematical terms

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM