Declare Vs Define
I'm sure this is a simple question and I'm not totally new to programming, just new to C++.
Can anyone explain what is a definition Vs what is a declaration. Just when I think it's cut and dry, the tutorial I'm reading confuses me. Is it... int num(void); a declaration and int num(void) a definition? { Do some stuff; } Thanks in advance |
Don't quote me....
You can define a word to mean something---maybe a bit like defining aliases. You **declare** variables (including the type of variable). Part of this is defining the storage space to be used. |
In C, the official term is declaration, and declare means to associate an identifier with an object (function, variable, type). But it is much more complicated than this, because there can be defining declarations and referencing declarations, and this all depends upon the translation unit (eg. a C source file).
An identifier can be declared as external, and may be followed by a definition. This definition helps the compiler in doing what are called forward references. For more precise understanding, see the C Reference Manual. |
In short, yes.
A declaration for a function is also called a prototype and it informs the compiler of your intent to define and to use it. A definition for a function is the body (code) associated with the prototype. |
Quote:
Forward declaration or prototypes are made for one reason: if you use a given function at one point, the program needs to know the basic data structures that that function will be handling (for both input and return types), but it's not necessary to know the whole code of the function at compile time, since the code will be dynamically linked at a later stage. You can do forward declarations in .h files and include them elsewhere, so you can use the functions anywhere in your program. Then you can develop that function into a regular C/C++ file. |
Quote:
A #define creates a label which will be used in the C or C++ source file, and wherever the compiler finds that label, it will replace the label with the value specified in the define, then it will proceed with the compile. This does not specify storage, and does not declare a variable; it merely is a label that is substituted at compile time. Thus, for instance, you might declare an array such as int myarray[20]; and later loop over that array: for(ii = 0;ii < 20; ii++) { loop de loop } In this case, you have twice referenced the size of this array; once when you declare it and again to loop over it. So, a few months from now, you have to change the size of that array. You change the declaration to 24 and don't remember to change the loop counter. You have just introduced a bug into your program. What you could have done was this: #define MYARRAYSIZE 20 then int myarray[MYARRAYSIZE]; then for(ii = 0;ii < MYARRAYSIZE; ii++){ blah blah } Doing it this way makes it easy to change things later; you just change the 20 to 24 in the #define statement. #define is also used to control compilation. For instance, I have a program that has code in it like this: It will either say this: #define MS_WINDOWS_TARGET or it will say this: #define LINUX_TARGET then later in the code we see things like this: #ifdef MS_WINDOWS_TARGET scrwidth = GetSystemMetrics(SM_CXSCREEN); #endif #ifdef LINUX_TARGET scrwidth = linux_screen_x_dim(); #endif At compile time, one or the other scrwidth statement is compiled, depending on whether I am compiling for Linux or for Windows. Elsewhere in the code, the entire linux_screen_x_dim() function will be compiled or not depending on the target system. Hence the difference between a declaration and a define. Edit: After re-reading OP's question, I think you were right; that is what he was asking. Oh well. |
Quote:
Quote:
|
Quote:
Quote:
|
Thanks all,
This helps. It doesn't solve my coding problem, but with this help, I can track down my miscode on my own. Thanks again for taking time. Ace |
Do you want to explain the problem or show the error?
|
Quote:
Declarations and definitions extend to classes, also. You can declare a class by simply using class a_class;, but you define it by giving it a body with {};. The simplest way to put it is that a declaration allows you to utilize a symbol name, whereas the definition is used to build the underlying binary code for it. In the case of classes, a declaration is enough to allow you to store a pointer to an object, but you need the definition of the class present in order to use its member functions (though those will be declared and probably not defined in the class' definition.) ta0kira |
Verbatim from the C Reference Manual:
Quote:
|
I don't dispute that part. I was remarking about your use of "definition" in the second sentence in the context of forward references, also known as forward declarations. I don't think your second sentence in what I quoted previously relates to the first.
ta0kira |
Another way to look at it.
Another way to look at it is that the definition is the instantiation of the object, whether a variable or a function. There can only be one definition, and there must be one in order to actually use it.
The declaration can be made many time; you're just declaring that it exists (is defined) someplace. This is for the compiler, and it will trust you, unlike the linker. The linker doesn't care about declarations, that's just talk. It cares about the definition, where the rubber hits the road. If you try to use something that isn't defined, the linker will call you on it. Likewise, if something is defined more than once. |
Mr C,
Thanks for the offer. After rereading the errors in detail (vs skimming and pick out highlights) I found most of my issues were typos LOL. I am working through Thinking in C++, 2nd ed. Volume 1 ©2000 by Bruce Eckel It leads me to another question. I am using Slackware 12.0 with KDE. I want to use the KDeveloper but it generates so much overhead. I just want to compile and run the simple exercises in the book. I don't need to track version and author information. Is there a way of turning off all the stuff like configuration file generation? Currently I am using KWrite and g++ from the shell prompt. Its not too bad, but correcting a simple typo can be an problem. Not to mention I'm eventually going to want to use the debugger and trace options. Thanks again all Ace |
All times are GMT -5. The time now is 09:12 AM. |