Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
A variable is something to store data in.
This data can be of any data type. The name of the
variable is called its identifier.
In the code example below, there are two variables, 'number' and 'i':
This produces the following screen output:
0 : 0
1 : 1
2 : 3
3 : 6
4 : 10
5 : 15
6 : 21
7 : 28
8 : 36
9 : 45
|
The scope of the variable 'number'
is from the line it is declared to
the end of main.
The scope of the variable 'i' is
inside the for-loop.
- Bjarne Stroustrup. Programming. 2009. ISBN: 978-0-321-54372-1. Chapter 5.9.1: 'Use meaningful names'
- Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Item 6.5.10: 'Don't declare a variable until you have a value to initialize it with'.
- Bjarne Stroustrup. Programming. 2009. ISBN: 978-0-321-54372-1. Chapter 5.9.1: 'Use meaningful names'
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Paragraph 19: 'Always initialize variables'
- Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Paragraph 18: 'Declare variables as locally as possible'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 6.6. Advice, page 169: '[21] Avoid uninitialized variables'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 9.8. Advice, page 240: '[1] Don't declare a variable until you have a value to initialize it with'
- Paul Deitel, Harvey Deitel. C++11 for progrgrammers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 2.4, Error Prevention Tip 2.1. page 25: 'Although it is not always necessary to initialize every variable explicitly, doing so will help you avoid many kinds of problems.'
- Paul Deitel, Harvey Deitel. C++11 for progrgrammers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 2.4, Good Programming Practice 2.3. page 26: 'Declare only one variable in each declaration and provide a comment that explains the variable's purpose in the program.'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 6.6. Advice. page 169: '[11] Declare one name (only) per declaration'
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
