The meaning of both eludes me.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
To understand the nouns, let's focus on the verbs first.
declare - to announce officially; proclaim
define - to show or describe (someone or something) clearly and completely
So, when you declare something, you just tell what it is.
This line declares a C function called
sum
that takes two arguments of typeint
and returns anint
. However, you can't use it yet.When you provide how it actually works, that's the definition of it.
A declaration presents a symbol name to the compiler. A definition is a declaration that allocates space for the symbol.
Declaration means give name and type to a variable (in case of variable declaration) eg:
or give name,return type and parameter(s) type to a function without body(in case of function declaration)
eg:
whereas definition means assign value to a variable (in case of variable definition). eg:
or provide/add body(functionality) to a function is called function definition.
eg:
many time declaration and definition can be done together as:
and
In above cases we define and declare variable i and function max()
A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:
A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:
A definition can be used in the place of a declaration.
An identifier can be declared as often as you want. Thus, the following is legal in C and C++:
However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.
Since the debate what is a class declaration vs. a class definition in C++ keeps coming up (in answers and comments to other questions) , I'll paste a quote from the C++ standard here.
At 3.1/2, C++03 says:
3.1/3 then gives a few examples. Amongst them:
To sum it up: The C++ standard considers
struct x;
to be a declaration andstruct x {};
a definition. (In other words, "forward declaration" a misnomer, since there are no other forms of class declarations in C++.)Thanks to litb (Johannes Schaub) who dug out the actual chapter and verse in one of his answers.
My favorite example is "int Num = 5" here your variable is 1. defined as int 2. declared as Num and 3. instantiated with a value of five. We
A class or struct allows you to change how objects will be defined when it is later used. For example
When we learn programming these two terms are often confused because we often do both at the same time.
This is going to sound really cheesy, but it's the best way I've been able to keep the terms straight in my head:
Declaration: Picture Thomas Jefferson giving a speech... "I HEREBY DECLARE THAT THIS FOO EXISTS IN THIS SOURCE CODE!!!"
Definition: picture a dictionary, you are looking up Foo and what it actually means.