This article looks at some of the basic data types that are built into C++. If you're learning how to use C++, you will want to keep reading, since you'll be using these data types in all of your programs. It is taken from chapter two of the book Beginning ANSI C++: The Complete Language, by Ivor Horton (Apress, 2004; ISBN: 1590592271).
IN THIS CHAPTER, you’ll look at some of the basic data types that are built into C++ and that you’re likely to use in all your programs. You’ll also investigate how to carry out simple numerical computations. All of C++’s object-oriented capability is founded on the basic data types built into the language, because all the data types that you’ll create are ultimately defined in terms of the basic types. It’s therefore important to get a good grasp of using them. By the end of the chapter, you’ll be able to write a simple C++ program of the traditional form: input – process – output.
In this chapter, you’ll learn about
Data types in C++
What literals are and how you define them in a program
Binary and hexadecimal representation for integers
How you declare and initialize variables in your program
How calculations using integers work
Programming with values that aren’t integers—that is, floating-point calculations
How you can prevent the value stored in a variable from being modified
How to create variables that can store characters
Data and Data Types
C++ is a strongly typed language. In other words, every data item in your program has a type associated with it that defines what it is and your C++ compiler will make extensive checks to ensure that, as far as possible, you use the right data type in any given context and that when you combine different types, they’re made to be compatible. Because of this type checking, the compiler is able to detect and report most errors that would arise from the accidental interpretation of one type of data as another or from attempts to combine data items of types that are mutually incompatible.
The numeical values that you can work with in C++ fall into two broad categories: integers (in other words, whole numbers) and floating-point values, which can be fractional. You can’t conclude from this that there are just two numerical data types, however. There are actually several data types in each of these categories, and each type has its own permitted range of values that it can store. Before I get into numerical types in detail, let’s look at how you carry out arithmetic calculations in C++, starting with how you can calculate using integers.