Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
09-12-2008, 04:00 PM
|
#1
|
|
Member
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.0_64
Posts: 280
Rep:
|
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
|
|
|
|
09-12-2008, 04:19 PM
|
#2
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
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.
|
|
|
|
09-12-2008, 04:37 PM
|
#3
|
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
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.
|
|
|
|
09-12-2008, 05:51 PM
|
#4
|
|
Senior Member
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,375
Rep: 
|
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.
|
|
|
|
09-12-2008, 06:21 PM
|
#5
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,965
|
Quote:
Originally Posted by graemef
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.
|
This is the most useful definition probably for the question that the OP made.
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.
|
|
|
|
09-12-2008, 07:04 PM
|
#6
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
Quote:
|
This is the most useful definition probably for the question that the OP made.
|
I am not sure about that, though you may be right. When I read the OP's question, I immediately thought about the preprocessor #define statements, which no one here has addressed.
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.
Last edited by jiml8; 09-12-2008 at 07:11 PM.
|
|
|
|
09-12-2008, 10:22 PM
|
#7
|
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Quote:
Originally Posted by jiml8
I am not sure about that, though you may be right. When I read the OP's question, I immediately thought about the preprocessor #define statements, which no one here has addressed.
...
Edit: After re-reading OP's question, I think you were right; that is what he was asking. Oh well.
|
I agree with your edit: there's nothing that indicates or implies #define macro substitution was intended.
Quote:
Originally Posted by jiml8
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.
|
A technicality: #define is not a label, and the compiler never sees it. #define is simple lexical macro substitution, interpreted and replaced by the pre-processor, traditionally cpp, which runs before the compilation phase.
|
|
|
|
09-12-2008, 11:19 PM
|
#8
|
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 3,965
|
Quote:
Originally Posted by Mr. C.
I agree with your edit: there's nothing that indicates or implies #define macro substitution was intended.
|
Yep. The OP didn't use a clear terminology, but he was obviously speaking about forward declarations.
Quote:
|
A technicality: #define is not a label, and the compiler never sees it. #define is simple lexical macro substitution, interpreted and replaced by the pre-processor, traditionally cpp, which runs before the compilation phase.
|
Also true, technically speaking, the compiler never gets to know about #defines. All the substitutions are done before the compiler gets into scene.
|
|
|
|
09-13-2008, 07:25 AM
|
#9
|
|
Member
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.0_64
Posts: 280
Original Poster
Rep:
|
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
|
|
|
|
09-13-2008, 11:13 AM
|
#10
|
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Do you want to explain the problem or show the error?
|
|
|
|
09-13-2008, 12:26 PM
|
#11
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
Quote:
Originally Posted by Mr. C.
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.
|
Do you mean declaration here? There can only be one definition but multiple declarations, so long as they match. Are you talking about something like extern void function() { }?
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
|
|
|
|
09-13-2008, 12:58 PM
|
#12
|
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Verbatim from the C Reference Manual:
Quote:
|
...if an an identifier is declared as being external, that declaration may be followed by a definition (section 4.8) of the name later in the program, assuming that the definition assigns the same type to the name as the external declaration(s). This exception allows the user to generate valid forward references to variables and functions.
|
As I indicated, it is more complicated than a simple paragraph will allow: "some aspects of C's declarations are difficult to understand without a knowledge of C's type system". An entire chapter is dedicated to explaining Declarations. I've used C as the basic model in my descriptions, as C++ has its roots in C.
|
|
|
|
09-13-2008, 01:55 PM
|
#13
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,967
Rep: 
|
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
|
|
|
|
09-13-2008, 11:40 PM
|
#14
|
|
LQ Newbie
Registered: Sep 2008
Location: Left Coast, USA
Distribution: Ubuntu Hardy
Posts: 15
Rep:
|
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.
|
|
|
|
09-14-2008, 06:18 PM
|
#15
|
|
Member
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.0_64
Posts: 280
Original Poster
Rep:
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:04 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|