Thread: Terms used in C

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    1

    Terms used in C

    Hi,

    I'm kinda confused about the terms used in C. Appreciate someone can explain to me what's the difference when we talk about defining, declaring and initialising a variable?

    Thanks alot

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    declaring (int x;)
    initializing( int x = 0;)

    I think that's how it oges. Not sure about defining.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    What he ^^ said. AFAIK, declaration of the variables is relevant only with extern storage class, e.g.:

    extern int i; /* declaration */
    int i=0; /* definition */

    HTH,
    $ENV: FreeBSD, gcc, emacs

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I learned it like this:
    Code:
    int i; /* initialization */
    int i=0; /* declaring */
    i = 0; /* definition */
    I may be wrong, (probably am) but the way I look at it.
    Initializing sets it up for the rest of the program. Declaring it is basically giving it a value right when it is created so its not random. And defining it is assigning a value to it when it has already been created.

    That's just the way I see it.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    This is the way I view it:
    Code:
    int i; //Declaring
    int i = 0; //Defining
    i = 0; //Initializing
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    In terms of variables (objects) only:

    A declaration describes the nature of a variable.

    A definition is also a declaration, but it actually reserves memory for the variable.

    Initialization specifies an initial value for an object at its definition (which is also a declaration, see above). This is not the same thing as simple assignment.

    Here is the exact wording from the C language standard:

    6.7 Declarations:
    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;98) for an enumeration constant or typedef name, is the (only) declaration of the identifier.

    6.7.8 Initializations:
    An initializer specifies the initial value stored in an object.

    Working off of the running example:
    Code:
    int i;        /* declaration (might be definition) */
    int i = 0;    /* definition (also a declaration) and initialization */
    i = 0;        /* simple assignment */
    The first one is a bit tricky, because it really depends on a few other things to determine what this declaration really is. Namely, we need to know whether this is at file scope (outside of any functions) or block scope (local variable).

    If it is at file scope, then it would be considered a "tentative definition". If it had an initializer like the second example, then it would be a "true definition". Objects declared at file scope can be declared many times, but they can only be defined once.

    If it is at block scope, then it is a true definition, because automatic objects are defined when they are declared (they can only have one declaration).

    This will bake your noodle, but if you can figure out why it is legal, then you probably have understood this explanation:
    Code:
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int main(void) { return i=0; }int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i = 100;int i;int i;int i;
    HTH and I hope I got this all correct,
    Will on his first post

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    whoie, you are a misinformation machine!

    Code:
    int i;        /* declaration (not initialized)*/
    int i = 0;    /* redeclaration= error */
    i = 0;        /* simple assignment */
    You can't declare a variable with the same name more than once in a scope. You could do this...
    Code:
    int i;        /* declaration (might be definition) */
    {
         int i = 0;    /* definition (also a declaration) and initialization */
    }
    i = 0;        /* simple assignment */
    But that is a completely different piece of cake.

    This, on the other hand, is complete and total rubish. Did you even try to compile it? I got 56 errors.
    Code:
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int main(void) { return i=0; }int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i;int i;int i;int i;int i;
    int i;int i;int i = 100;int i;int i;int i;
    C:\Text1.cpp(2) : error C2086: 'i' : redefinition

    When providing sample code, please either make sure you know what you're saying/doing or test to make sure that it compiles and works. We want to keep this site as accurate and useful as possible.

    Oh, and don't feel bad. Lots of new people do it.
    Away.

  8. #8
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>Will on his first post
    Whoo! Welcome to Cprogramming,com Whoie. Not to boost your ego or anything but the only reason I go to programmersheaven with any regularity is to read your posts.

    >>whoie, you are a misinformation machine!
    I couldn't find any mistakes.

    >>You can't declare a variable with the same name more than once in a scope.
    In C you can, C++ has a one definition rule though. Are you sure you're compiling your test as C?

    >>C:\Text1.cpp(2) : error C2086: 'i' : redefinition
    Nope.

    >>Oh, and don't feel bad. Lots of new people do it.
    And some old people too.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    blackrat364:
    Please consider your noodle fully baked . Seriously though, I take it as a good sign that someone would call me on something that is incorrect, and I am in complete agreement with the notion of keeping this site accurate. All I ask in the future is to please check your facts first before correcting me, and I promise to do the same when the shoe is on the other foot. In this case, I assumed the distinction was already made because it was the C board specifically, but being that I am new here, that may not be the way it works. In any case, I should have mentioned that what I said was not applicable to C++. Regardless, my wife appreciated it, because she has been calling me a "misinformation machine" all stinkin' day!

    Salem:
    That's a handy link there, thanks! <adds it to bookmarks>

    Casey:
    No need to worry about the ego, my wife ensures that it is always within acceptable limits , but that was a very nice thing to say. Thanks!

  10. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    whoie, it was entirely my bad. I chronically forget that I'm posting on the C board and not the C++ board, so I compile everything for C++ You should have seen the time that I posted something about classes on the C board.

    Geeze, I'm a moron. I should just shut my mouth.
    Away.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >>>I couldn't find any mistakes.

    ?!

    Loading.....
    ( Trying to be a good C Programmer )

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    30
    The way i view it:


    Code:
    int i;        /* declaration */
    int i = 0;    /* declaration and initialization */
    i = 0;        /* assigning a value to i */

  13. #13
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11
    According to my second edition C/C++ Programmer's Reference (Herbert Schildt), it goes like this:

    Code:
    type variable_name; /* declaration - all variables must be declared prior to use */
    type variable_name = value; /* initialization - a variable can be initialized by following it's name with an equal sign and an initial value.
    Declaration vs. Definition
    Two terms are commonly confused in C/C++ programming: declaration and definition. Here is what they mean. A declaration specifies the name and type of an object. A definition allocates storage for it. The definitions apply to functions, too. A function declaration (prototype) specifies the return type, name and parameters of a function. The function itself (that is, the function with its body) is its definition.

    In many cases, a declaration is also a definition. For example, when a non-extern variable is declared, it is also defined. Or, when a function is defined prior to its first use, its definition also serves as its declaration.
    Chaos, panic, pandemonium... My work here is done!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  2. how many terms
    By rams in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:10 AM
  3. Food Terms
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-01-2001, 06:59 PM
  4. C++ terms Questions
    By DramaKing in forum C++ Programming
    Replies: 8
    Last Post: 11-12-2001, 11:13 PM
  5. Terms
    By Drakon in forum Game Programming
    Replies: 7
    Last Post: 09-27-2001, 06:24 PM