LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Declare Vs Define (https://www.linuxquestions.org/questions/programming-9/declare-vs-define-669556/)

Ace Blackwell 09-12-2008 04:00 PM

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

pixellany 09-12-2008 04:19 PM

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.

Mr. C. 09-12-2008 04:37 PM

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.

graemef 09-12-2008 05:51 PM

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.

i92guboj 09-12-2008 06:21 PM

Quote:

Originally Posted by graemef (Post 3278761)
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.

jiml8 09-12-2008 07:04 PM

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.

Mr. C. 09-12-2008 10:22 PM

Quote:

Originally Posted by jiml8 (Post 3278795)
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 (Post 3278795)
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.

i92guboj 09-12-2008 11:19 PM

Quote:

Originally Posted by Mr. C. (Post 3278904)
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.

Ace Blackwell 09-13-2008 07:25 AM

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

Mr. C. 09-13-2008 11:13 AM

Do you want to explain the problem or show the error?

ta0kira 09-13-2008 12:26 PM

Quote:

Originally Posted by Mr. C. (Post 3278711)
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

Mr. C. 09-13-2008 12:58 PM

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.

ta0kira 09-13-2008 01:55 PM

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

linfidel 09-13-2008 11:40 PM

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.

Ace Blackwell 09-14-2008 06:18 PM

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.