Could someone tell me the difference between a declaration and definition of a variable????
Thx in advance
Could someone tell me the difference between a declaration and definition of a variable????
Thx in advance
Well.. I dunno about a "variable", but here's the diff for functions (applies to other things, of course)
Code:void MyFunc (void); // declaration int main() { MyFunc(); return(0); } void MyFunc (void) // definition { cout << "MyFunc\n"; }
I think (not 100% sure) that when something (a variable, struct)is defined memory is not allocated. When it is declared memory is allocated.
Colin
I agree with Colin:
You "define" a variable type. Here is a sample user defined variable type:
struct Sample
{
int num;
char name[80];
};
You "declare" an instance of some type, said instance often called an object. Here is one way (there are others) to declare an instance of a type, in this case an instance of the struct type called Sample.
Sample sample1;
Memory is set aside when an object is declared, not when a type is defined. The definition tells the compiler how much memory to allocate for a given type, but doesn't the compiler to actually allocate any memory yet.
Here's how I look at it:
int x; // declare a variable
x = 345; // define a variable
int x; //declare variable
x = 35;//assign a value to variable
int y = 24;//initialize a variable with a value, looks like an assignment, but it's not
the type called int is defined in iostream.h someplace, I think.
>Could someone tell me the difference between a declaration and definition of a variable????
A declaration gives the compiler information about the type of an identifier. A definition is a declaration, but a declaration isn't always a definition. A definition means that you are allocating storage for a variable and optionally initializing it with a value. Consider a variable with file scope:
/* The definition can only occur once in the program
*/
int value = 32;
/* The declaration could be made anywhere in the program.
** Even in the same file that variable was defined without effecting
** the definition.
*/
extern int value;
-Prelude
My best code is written with the delete key.
Think of a declaration as CREATING and definition as USING.
//declare your variables
int i;
int j;
long k;
//define your variables.
i=36;
j=52;
k=266;
i=j;
Header files hold the declarations of functions, variables, and classes. Those functions, variables, and classes can be defined in a separate CPP file.
My Avatar says: "Stay in School"
Rocco is the Boy!
"SHUT YOUR LIPS..."
>Think of a declaration as CREATING and definition as USING.
No, don't. It's attempts at abstraction such as this that cause confusion. "Well, it's like this, so think of it like this even though it's completely different"
Note that I said a definition can only occur *once* in a program. You can allocate the memory for a variable *once* for that identifier. Using, defining, and declaring are all different. The differences may seem subtle and useless, but they are very important.
-Prelude
My best code is written with the delete key.