Declaring and Initializing - What are they?

I REALLY should know this (but I don't) because I've been programming for quite some time now, but I have been on and off due to school.
I think that declaring a variable is doing something like:
int x = 7; then cout << "Blah, blah" << x << endl;
Correct me if I'm wrong on this.
(Although I feel like I just answered my own question)
extern int x; // declaration only
int x; // declaration and definition
int x = 7; // declaration, definition and initialization
Last edited on
closed account (zb0S216C)
Declaring an identifier means bringing an identifier into scope. In other words, telling the compiler that a variable with the identifier "Variable" exists, for example. Note that a definition, and a declaration of an identifier is the same thing, but the two are used interchangeably.

Initialising a variable means to give a variable a value during its declaration. The difference between initialisation and assignment is quite distinct, since initialisation only applies to a declaration context. You should be aware that initialisation is good practice.

Wazzak
@Framework
Note that a definition, and a declaration of an identifier is the same thing, but the two are used interchangeably.


It is a wrong statement. Not every declaration of a variable is at the same time a definition.

See examples in my previous message.

Another example

struct A; // declaration only
struct A { int a; }; // declaration and definition

And one more example

1
2
3
4
struct A
{
   static int a[]; // declaration only
};

int A::a[10]; // declaration and definition
Last edited on
closed account (zb0S216C)
@Vlad: The OP was referring to variables, not structures. Maybe I should've been more clear, just for you.

Wazzak
extern int x; is declaration not a definition. The decllaration and the definition are exact notions in the C++ standard. The same is valid for a static data member declared in a class. When you are declaring a static data member you can declare it with incomplete type because it is only a declaration not definition. See my example with the static data member declared as array.
closed account (zb0S216C)
@Vlad: The following statements are forward declarations, not ordinary declarations:

1
2
3
extern int A; // Does not introduce a new identifier.
struct X;
int Function( );

Wazzak
There is no such a notion as ordinary declaration in C++.
You are wrong. For example

extern int A; // does introduce a new identifier in a translation unit

int function(); // it is a declaration

using std::string; // it is a declaration

and so on including a declaration of a static data member in a class.
Thanks for the info, guys :D The little argument/discussion you guys were having amused me a little :) And framework, how could I practice programming with initialization? Would you mind giving some example code to work with?
@OP:

Something like this is a good practice:

1
2
3
4
5
6
int x=0;   //declared and initialized

cout<<"Enter a number: ";
cin>>x;
int *temp=new[x];
cout<<x;


x is initialized, in case you make a mistake and let the user cin>> in a diferent variable. If it was not initialized, you would have gotten some error
closed account (zb0S216C)
DJLad15 wrote:
"And framework, how could I practice programming with initialization?"

Remembering to initialise your variables is always good practice, and the more you do it, the more likely it will be your second nature. Initialisation can happen a few ways, but the way you initialise a variable depends on where, and how it was declared. Below is some cases of initialisation:

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
33
// ------------------
// Type A
// ------------------
struct Sample
{
    Sample(int const InitValue = 0)
        : Member(InitValue) // Initialiser-list
    { }
    int Member;
};

// If you gave "Sample::Member" a value within the body of the "Sample" constructor,
// you would be assigning a value to "Sample::Member", and not initialising it.

// ------------------
// Type B
// ------------------
struct Sample
{
    static int StaticMember;
};

int Sample::StaticMember(0); // Initialisation.

// ------------------
// Type C
// ------------------
int Variable(0); // "Variable = 0" is the same.

// ------------------
// Type D
// ------------------
int Array[3] = {1, 2, 3};

Wazzak
Topic archived. No new replies allowed.