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
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
declaring (int x;)
initializing( int x = 0;)
I think that's how it oges. Not sure about defining.
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
I learned it like this:
I may be wrong, (probably am) but the way I look at it.Code:int i; /* initialization */ int i=0; /* declaring */ i = 0; /* definition */
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
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
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:
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).Code:int i; /* declaration (might be definition) */ int i = 0; /* definition (also a declaration) and initialization */ i = 0; /* simple assignment */
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:
HTH and I hope I got this all correct,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;
Will on his first post
whoie, you are a misinformation machine!
You can't declare a variable with the same name more than once in a scope. You could do this...Code:int i; /* declaration (not initialized)*/ int i = 0; /* redeclaration= error */ i = 0; /* simple assignment */
But that is a completely different piece of cake.Code:int i; /* declaration (might be definition) */ { int i = 0; /* definition (also a declaration) and initialization */ } i = 0; /* simple assignment */
This, on the other hand, is complete and total rubish. Did you even try to compile it? I got 56 errors.
C:\Text1.cpp(2) : error C2086: 'i' : redefinitionCode: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;
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.
>>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.
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!
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.
>>>I couldn't find any mistakes.
?!
Loading.....
( Trying to be a good C Programmer )
The way i view it:
Code:int i; /* declaration */ int i = 0; /* declaration and initialization */ i = 0; /* assigning a value to i */
According to my second edition C/C++ Programmer's Reference (Herbert Schildt), it goes like this:
Declaration vs. DefinitionCode: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.
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!