© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2023
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-9274-7_22

22. Exercises

Slobodan Dmitrović1  
(1)
Belgrade, Serbia
 

Automatic Storage Duration

Write a program that defines two variables of type int with automatic storage duration (placed on the stack) inside the main function scope.
#include <iostream>
int main()
{
    int x = 123;
    int y = 456;
    std::cout << "The values with automatic storage durations are: " << x << " and " << y;
}
Output:
The values with automatic storage durations are: 123 and 456

We can use the following image to visualize the placement of automatic storage objects called x and y:

An illustration of the memories ...

Get Modern C++ for Absolute Beginners: A Friendly Introduction to the C++ Programming Language and C++11 to C++23 Standards now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.