Declaration and initialization


With the declaration we name a variable and make it known to the compiler. Since each variable name of a data type must be unique, the compiler can also catch the error when trying to declare two variables with the same type and the same name.


The definition means that a memory area is allocated to a variable. The memory area has a unique address and is used to store values. However, a declaration without definition can also take place. Then both concepts are combined.


If we have declared a variable, it has a random value – depending on what is currently in the allocated memory area. Normally you don’t want to work with such random values. Therefore we can use initialization to set the variable to an initial value. Variables should always be initialized to avoid working with a random value.

Code Example

#include <iostream>
using namespace std;

int main () {
    // variable declaration
    int integerNumber; 

    // variable declaration & initialization
    short shortNumber = 10000; 
    unsigned int unsignedInteger = 10;
    long longNumber = 10000000000;
    unsigned long unsignedLong = 234342;
    double doubleNumber = 5.534;
    long double longDouble = 53453.534;
    float floatNumber = 6.767f;
    char symbol = 'c';
    bool isBoolean= true;
    string charString = "My first string";
    int myArray[] = {16, 24, 35, 41};

    // variable initialization
    integerNumber = 1000000000;

    // variables output
    cout << shortNumber << "\n"
         << integerNumber << "\n"
         << unsignedInteger << "\n"
         << longNumber << "\n"
         << unsignedLong << "\n"
         << doubleNumber << "\n"
         << longDouble << "\n"
         << floatNumber << "\n"
         << symbol << "\n"
         << isBoolean << "\n"
         << charString << "\n"
         << myArray[3] << "\n"
         << endl;
My first string

Code Explanation

  1. The line #include <iostream> includes the iostream library, which provides input and output functionality in C++.
  2. The line using namespace std; declares that we’re using the std namespace, which contains various standard C++ functions and objects, including cout.
  3. The int main() function is the entry point of the program. It returns an integer value, typically 0, indicating successful execution.
  4. Inside the main() function, several variables are declared and initialized with different data types and values.
  5. The variables declared are:
    • integerNumber, which is an int variable declared without initialization.
    • shortNumber, which is a short variable initialized with the value 10000.
    • unsignedInteger, which is an unsigned int variable initialized with the value 10.
    • longNumber, which is a long variable initialized with the value 10000000000.
    • unsignedLong, which is an unsigned long variable initialized with the value 234342.
    • doubleNumber, which is a double variable initialized with the value 5.534.
    • longDouble, which is a long double variable initialized with the value 53453.534.
    • floatNumber, which is a float variable initialized with the value 6.767f.
    • symbol, which is a char variable initialized with the character 'c'.
    • isBoolean, which is a bool variable initialized with the value true.
    • charString, which is a string variable initialized with the string value "My first string".
    • myArray, which is an int array initialized with the values {16, 24, 35, 41}.
  6. The line integerNumber = 1000000000; initializes the previously declared integerNumber variable with the value 1000000000.
  7. The cout statements use the << operator to insert the variable values into the output stream. Each variable’s value is displayed on a separate line.
  8. Finally, the line << endl; uses the << operator to insert a newline character into the output stream.