C++ Variable Declaration, Definition, and Initialization

Introduction

Introducing a name into a current scope is called a declaration.

In a declaration, we prepend the variable name with a type name.

Declaration examples:

int main() 
{ 
    char c; 
    int x; 
    double d; 
} 

We can declare multiple names on the same line:

int main() 
{ 
    int x, y, z; 
} 

If there is an initializer for an object present, then we call it an initialization.

We are declaring and initializing an object to a specific value.

We can initialize an object in various ways:

int main() 
{ 
    int x = 123; 
    int y{ 123 }; 
    int z = { 123 }; 
} 

The definition is also a declaration. Definition examples:

int main() 
{ 
    char c = 'a'; 
    int x = 123; 
    double d = 456.78; 
} 



PreviousNext

Related