LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 09-12-2008, 04:00 PM   #1
Ace Blackwell
Member
 
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.0_64
Posts: 284

Rep: Reputation: 30
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
 
Old 09-12-2008, 04:19 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 09-12-2008, 04:37 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
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.
 
Old 09-12-2008, 05:51 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
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.
 
Old 09-12-2008, 06:21 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Quote:
Originally Posted by graemef View Post
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.
 
Old 09-12-2008, 07:04 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
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.
 
Old 09-12-2008, 10:22 PM   #7
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Quote:
Originally Posted by jiml8 View Post
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 View Post
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.
 
Old 09-12-2008, 11:19 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Quote:
Originally Posted by Mr. C. View Post
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.
 
Old 09-13-2008, 07:25 AM   #9
Ace Blackwell
Member
 
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.0_64
Posts: 284

Original Poster
Rep: Reputation: 30
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
 
Old 09-13-2008, 11:13 AM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Do you want to explain the problem or show the error?
 
Old 09-13-2008, 12:26 PM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Mr. C. View Post
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
 
Old 09-13-2008, 12:58 PM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
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.
 
Old 09-13-2008, 01:55 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 09-13-2008, 11:40 PM   #14
linfidel
LQ Newbie
 
Registered: Sep 2008
Location: Left Coast, USA
Distribution: Ubuntu Hardy
Posts: 15

Rep: Reputation: 0
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.
 
Old 09-14-2008, 06:18 PM   #15
Ace Blackwell
Member
 
Registered: Mar 2004
Location: Kentucky, USA
Distribution: SlamD 12.1 / Slack 12.0 ~ 14.0_64
Posts: 284

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i declare a Vector in an IDL? alaios Programming 8 11-20-2006 12:42 AM
why C has two ways to declare structs? angustia Programming 5 04-15-2006 10:36 PM
declaration does not declare anything tristanm Programming 5 10-24-2005 04:00 PM
Help for perl--- Getopt::Declare arobinson74 Programming 1 08-20-2004 10:31 AM
How to declare constant strings in C/C++? Dark_Helmet Programming 3 05-29-2004 08:07 AM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration