Declarations vs Definitions

Ok so

i know what a declartion is its like

1
2
int i; // We have declared a variable called i


But what is a definition can you show me a simple excample of one like i did above but with a definition instead
closed account (EwCjE3v7)
That is a definition.

A declaration is something like this

1
2
extern int i: // a declaration of i
class something; // extern not allowed for class type 


You can also assign a reference to a declaration

Definition:

1
2
int i; // definition
class something = {};

http://www.cprogramming.com/declare_vs_define.html
Last edited on
Im still confused

int i; is a definition i thoguht i was a declartion

@YellowPyrmid

I think you have misunderstood the material in the link you provided.

@OP

Alex Allain wrote:
When you declare a variable, a function, or even a class all you are doing is saying: there is something with this name, and it has this type.


It can apply to variables, functions, classes etc:

1
2
3
4
int i; //storage set aside for this variable, not initialised contains garbage 

int MyFunction(); // the definition of this function is later in the file
// left out the class stuff it might be too advanced 


So there is no values assigned.

Defining means you assign a value to a variable, or define the code in the function, you are saying what it actually is:

1
2
3
4
5
6
 int i = 10; // declare & initialise (assign a value to )a variable

int MyFunction() { // define a function
    int a = 10;
   return a;
}


It is a really good idea to declare & initialise your variable in one line, do one variable per line:

1
2
double a = 10.0; // Ellipse semi-major axis
double b = 5.0;   // Ellipse semi-minor axis 


With program layout:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
// other include files as necessary

//function declarations
void      MyfunctionA(int a);
double  MyFunctionB();

int main () {

int a = 10;

   MyfunctionA(a);

   double b = 0;

   b = MyFunctionB();

   std::cout << "b is " << b << "\n";

   return 0;
}

// function definitions

void MyfunctionA(int a) {
   std::cout << "a is " << a << "\n";
}

double  MyFunctionB() {
   return 10.0;
}


Hope all goes well
closed account (EwCjE3v7)
Wait I`m wrong but look at this

http://msdn.microsoft.com/en-us/library/0e5kx78b.aspx

Contains the extern specifier but no initializer (objects and variables) or function body (functions). This signifies that the definition is not necessarily in the current translation unit and gives the name external linkage.


and if you scroll you can see

Examples of declarations that are also definitions are:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Declare and define int variables i and j.
int i;
int j = 10;

// Declare enumeration suits.
enum suits { Spades = 1, Clubs, Hearts, Diamonds };

// Declare class CheckBox.
class CheckBox : public Control
{
public:
            Boolean IsChecked();
    virtual int     ChangeState() = 0;
};


Some declarations that are not definitions are:


1
2
extern int i;
char *strchr( const char *Str, const char Target );
Last edited on
Defining means you assign a value to a variable, or

That would be initialisation. I think you meant allocating storage for the variable.
@Chervil

One could argue that defining a POD variable and initialisation are the same thing. I did not mean that defining is allocating storage - that is what declaration does.

1
2
int a; // allocate storage (declaration)
int b = 10; // allocate storage and initialise 


@YellowPyrmid

MSDN wrote:

A declaration introduces one or more names into a program. Declarations can occur more than once in a program. Therefore, classes, structures, enumerated types, and other user-defined types can be declared for each compilation unit. The constraint on this multiple declaration is that all declarations must be identical. Declarations also serve as definitions, except when the declaration:

1 Is a function prototype (a function declaration with no function body).

2 Contains the extern specifier but no initializer (objects and variables) or function body (functions). This signifies that the definition is not necessarily in the current translation unit and gives the name external linkage.

3 Is of a static data member inside a class declaration.

Because static class data members are discrete variables shared by all objects of the class, they must be defined and initialized outside the class declaration. (For more information about classes and class members, see Classes.)

4 Is a class name declaration with no following definition, such as class T;.

5 Is a typedef statement.



So I think you misread that a bit.

Declarations tell the compiler that a program element or name exists. Definitions specify what code or data the name describes. A name must be declared before it can be used.


Ok, that's fine. But I don't see how int i; can be a definition.

Examples of declarations that are also definitions are:


My interpretation:

i is declared but not defined (not initialised with anything)
j is declared and defined (initialised with value 10)

enum suits is declared and defined

1
2
3
4
5
6
7
// Declare class CheckBox.
class CheckBox : public Control
{
public:
            Boolean IsChecked();
    virtual int     ChangeState() = 0;
};

I wouldn't call that code a definition, in my mind a class declaration is what one puts in a header file, it's definition (that is it's function definitions) are in the implementation file .cpp.

Also I wouldn't consider CheckBox as defined because it is incomplete, never mind the pure virtual function, there is no definition for the IsChecked() function. If that function definition was inline then it would be.

A forward declaration of this class would just be class CheckBox;

TheIdeasMan wrote:
One could argue that defining a POD variable and initialisation are the same thing. I did not mean that defining is allocating storage - that is what declaration does.

I don't think one can make that argument. One can define without initializing, and a pure declaration allocates no storage.

Take, for instance:

1
2
3
4
struct A
{
    static int value ;
};


There is no storage allocated here. All we've told the compiler is that value exists, but it doesn't until we define it. And, if you don't define it, but have code that attempts to use it, the linker will complain to you about an undefined reference.


TheIdeaMan wrote:
Ok, that's fine. But I don't see how int i; can be a definition.

My interpretation:

i is declared but not defined (not initialised with anything)


i is defined because the compiler has allocated memory for it. We may take it's address, for instance. If it were not defined, we could not do that. Initialization is not definition.

Standard says in 1.8 [The C++ Object Model], emphasis mine.
IS wrote:
An object is created by a definition (3.1), by a new-expression (5.3.4)
or by the implementation (12.2) when needed.


3.1 [Definitions and declarations]:
A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification25 (7.5) and neither an initializer nor a functionbody, it declares a static data member in a class definition (9.2, 9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), it is a template-parameter (14.1), it is a parameter-declaration (8.3.5) in a function declarator that is not the declarator of a function-definition, or it is a typedef declaration (7.1.3), an alias-declaration (7.1.3), a using-declaration (7.3.3), a static_assert-declaration (Clause 7), an attributedeclaration (Clause 7), an empty-declaration (Clause 7), or a using-directive (7.3.4).
[ Example: all but one of the following are definitions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a; // defines a
extern const int c = 1; // defines c
int f(int x) { return x+a; } // defines f and defines x
struct S { int a; int b; }; // defines S, S::a, and S::b
struct X { // defines X
    int x; // defines non-static data member x
    static int y; // declares static data member y
    X(): x(0) { } // defines a constructor of X
};
int X::y = 1; // defines X::y
enum { up, down }; // defines up and down
namespace N { int d; } // defines N and N::d
namespace N1 = N; // defines N1
X anX; // defines anX 

whereas these are just declarations:
1
2
3
4
5
6
7
extern int a; // declares a
extern const int c; // declares c
int f(int); // declares f
struct S; // declares S
typedef int Int; // declares Int
extern X anotherX; // declares anotherX
using N::d; // declares d 

-- end example]

Last edited on
@cire

Cheers, thanks for all the great info - awesome advice as per usual :+)

++ThingsLearnt;

Regards
I am still sooo confused so

1
2
3
int i; // definiton or declartion

int c = 10; // decalrtion or definition 
int i; - A definition which also serves as a declaration.
int c=10; - A definition which also serves as a declaration.

In the first definition, there is no initialization. In the second, there is.
Last edited on
Topic archived. No new replies allowed.