C++ Demystified(c) A Self-Teaching Guide

Project: Determining the Size of Data Types

As discussed in the previous Data Types section, the size of each data type depends on the compiler and operating system you are using. In this project, you will find out the size of each data type on your system by using the sizeof operator.

The sizeof Operator

The sizeof operator is followed by parentheses, in which you place a data type. It returns the size in bytes of that data type.

For example, on my computer, the expression sizeof(int) returns 4. This means that on my compiler and operating system, the size of an int data type is 4 bytes.

Changing the Source File of Your Project

Try creating and running the next program using the steps you followed in Chapter 1 to create the Hello World! program. While you could start a new project, in this example, you will reuse the project you used in Chapter 1. It is good to know both how to create a new project and how to reuse an existing one.

  1. Start Visual C++.

  2. Use the File Open Solution menu command to display the Open Solution dialog box shown in Figure 2-5.

    Figure 2-5: Opening the Existing Solution

  3. Navigate to the folder where you saved the project (C:\temp\helloworld on my computer) and find the solution file. It has the extension .sln, which stands for solution. The solution file is helloworld.sln in Figure 2-5.

  4. Open the solution file. This should open your project.

  5. Display Solution Explorer using the View Solution Explorer menu command, and then click the Source Files folder to show the hello.cpp file, as depicted in Figure 2-6.

    Figure 2-6: Showing the Existing Source File in Solution Explorer

  6. Right-click the hello.cpp file and choose Remove from the shortcut menu (shown in Figure 2-7). Don t worry, this will not delete the file, but instead simply remove it from the project. You still will be able to use it later if you wish.

    Figure 2-7: Remove option on Shortcut Menu

  7. Right-click the Source Files folder and choose Add New Item from the shortcut menu. This will display the Add New Item dialog box, shown in Figure 2-8.

    Figure 2-8: Adding a New Source File to your Project

  8. Don t change the Location field, which holds the subfolder in which the project files are stored. Type the name of the new source file in the Name field, such as sizeof.cpp .

  9. When you are done, click the Open button. Figure 2-9 shows the new sizeof.cpp file in Solution Explorer.

    Figure 2-9: Solution Explorer showing the new .cpp file

Double-click sizeof.cpp in Solution Explorer to display the sizeof.cpp file in the code editing window. At this point, the sizeof.cpp is blank. In the next section, you will add code.

Code and Output

Write the following code in the source file you have created. I will explain the code in the following sections.

#include <iostream> using namespace std; int main(void) { cout << "Size of short is " << sizeof(short) << "\n"; cout << "Size of int is " << sizeof(int) << "\n"; cout << "Size of long is " << sizeof(long) << "\n"; cout << "Size of float is " << sizeof(float) << "\n"; cout << "Size of double is " << sizeof(double) << "\n"; cout << "Size of long double is " << sizeof(long double) << "\n"; cout << "Size of char is " << sizeof(char) << "\n"; cout << "Size of bool is " << sizeof(bool) << "\n"; return 0; }

Next, build and run the project, following the same steps you did for the Hello World! Project in Chapter 1. The resulting output on my computer is

Size of short is 2 Size of int is 4 Size of long is 4 Size of float is 4 Size of double is 8 Size of long double is 8 Size of char is 1 Size of bool is 1

Note  

The numbers displayed on your computer may be different, because the size of a data type depends on the particular compiler and operating system you are using, and yours may not be the same as mine.

Expressions

The line of code

cout << "Size of int is " << sizeof(int) << "\n";

displays the following output:

Size of int is 4

In essence, the code sizeof(int) is replaced by 4 in the output.

The code sizeof(int) is called an expression. An expression is a code statement that has a value, usually a value that has to be evaluated when the program runs. An example of an expression is 4 + 4, which has a value, 8, that would be evaluated when the program runs.

When the code runs, the expression sizeof(int) is evaluated as having the value 4, which then is outputted.

By contrast, the portion of the statement within double quotes, Size of int is , is outputted literally as Size of int is 4. There is no need for an evaluation. Instead, this is considered a literal string. The term string refers to the data type, a series of characters , and the term literal refers to the fact that the string is outputted literally, without evaluation. The string Hello World! in the cout statement in Chapter 1 also was a literal string.

Outputting an Expression

The expression sizeof(int) is separated by the stream insertion operator (<<) from the literal string Size of int is . If the code statement instead were

cout << "Size of int is sizeof(int)\n";

then the output would be quite different:

Size of int is sizeof(int)

The reason is sizeof(int), being encased inside the double quotes, would be treated as a literal string, not an expression, and therefore would not be evaluated, but instead displayed as is.

Since Size of int is is a literal string and sizeof(int) is an expression, they need to be differentiated before being inserted into the output stream. This differentiation is done by placing a stream insertion operator between the literal string and the expression.

Note  

The string Size of int is ends with a space between is and the following 4. Without that space, the output would be Size of int is4. You, as the programmer, have the responsibility to ensure proper spacing; C++ won t do it for you.

Escape Sequences

The string \n following the expression sizeof(int) is also a literal string, so it, too, is separated by a stream insertion operator from the sizeof(int) expression. However, \n is a special type of string called an escape sequence.

C++ has many escape sequences, though this may be the commonest one. This particular escape sequence causes the cursor to go to the next line for further printing. Without it, all the output would be on one line.

The \n in a string is not displayed literally by cout even though it is encased in double quotes. The reason is that the backslash signals cout that this is an escape sequence.

Table 2-6 shows some of the most common escape sequences.

Table 2-6: Common Escape Sequences

Escape Sequence

Name

What It does

\a

Alarm

Causes the computer to beep

\n

newline

Causes the cursor to go to the next line

\t

Tab

Causes the cursor to go to the next tab stop

\\

Backslash

Causes a backslash to be printed

\'

Single quote

Causes a single quote to be printed

\

Double quote

Causes a single quote to be printed

Категории