Placement of Variable Declarations

Declarations of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program. For example, in the program of Fig. 2.5, the declaration in line 9

int number1 = 0; // first integer to add (initialized to 0)

could have been placed immediately before line 14

std::cin >> number1; // read first integer from user into number1

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.