O'Reilly logo

Practical C++ Programming, 2nd Edition by Steve Oualline

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4. Basic Declarations and Expressions

A journey of a thousand miles must begin with a single step.

Lao-zi

If carpenters made buildings the way programmers make programs, the first woodpecker to come along would destroy all of civilization.

Anonymous

If you are going to construct a building, you need two things: the bricks and a blueprint that tells you how to put them together. In computer programming you also need two things: data (variables) and instructions (code). Variables are the basic building blocks of a program. Instructions tell the computer what to do with the variables.

Comments are used to describe the variables and instructions. They are notes by the author documenting the program so it is clear and easy to read. Comments are ignored by the computer.

In construction, before we can start we must order our materials: "We need 500 large bricks, 80 half-size bricks, and 4 flagstones." Similarly, in C++ you must declare all variables before you can use them. You must name each one of your "bricks" and tell C++ what type of "brick" to use.

After the variables are defined, you can begin to use them. In construction the basic structure is a room. By combining many rooms we form a building. In C++ the basic structure is a function, and functions can be combined to form a program.

An apprentice builder does not start out building the Empire State Building. He starts on a one-room house. In this chapter you will concentrate on constructing simple, one-function programs.

Basic Program Structure

The basic elements of a program are the data declarations, functions, and comments. Let's see how these can be organized into a simple C++ program.

The basic structure of a one-function program is:

/******************************************************** 
 * Heading comments                                     * 
 ********************************************************/ 
data declarations 
int main(  ) 
{ 
    executable statements 
    return(0); 
}

The heading comments tell the programmer all about the program. The data declarations describe the data that the program is going to use.

Our single function is named main. The name main is special, because it is the first function called. Any other functions are called directly or indirectly from main. The function main begins with:

int main(  ) 
{

and ends with:

    return(0); 
}

The line return(0); is used to tell the operating system that the program exited normally (status=0). A nonzero status indicates an error—the bigger the return value, the more severe the error. Typically 1 is used for most simple errors, such as a missing file or bad command-line syntax.

Now let's take a look at the "Hello World" program (Example 3-1).

At the beginning of the program is a comment box enclosed in /* and */. Next we have the line:

#include <iostream>

This tells C++ that we want to use the standard input/output system. (The proper name for this is the I/O streams module.) This is a type of data declaration.[1]

The main routine contains the instruction:

    std::cout << "Hello World\n";

This instruction is an executable statement telling C++ to write the message "Hello World" on the screen. The special character sequence \n tells C++ to write out a newline character. C++ uses a semicolon to end a statement in much the same way we use a period to end a sentence. Unlike line-oriented languages such as BASIC, the end of a line does not end a statement. The sentences in this book can span several lines—the end of a line is treated as a space separating words. C++ works the same way. A single statement can span several lines. Similarly, you can put several sentences on the same line, just as you can put several C++ statements on the same line. However, most of the time your program is more readable if each statement starts on a separate line.

We are using the standard object std::cout (console out) to output the message. A standard object is a generally useful C++ object that has already been defined and put in the standard library. A library is a collection of class definitions, functions, and data that have been grouped together for reuse. The standard library contains classes and functions for input, output, sorting, advanced math, and file manipulation. See your C++ reference manual for a complete list of library functions and standard objects.

"Hello World" is one of the simplest C++ programs. It contains no computations, merely sending a single message to the screen. It is a starting point. Once you have mastered this simple program, you have done many things right. The program is not as simple as it looks. But once you get it working, you can move on to create more complex code.

Simple Expressions

Computers can do more than just print strings. They can also perform calculations. Expressions are used to specify simple computations. C++ has the five simple operators listed in Table 4-1.

Table 4-1. Simple operators

Operator

Meaning

*

Multiply

/

Divide

+

Add

-

Subtract

%

Modulus (remainder after division)

Multiply (*), divide (/), and modulus (%) have precedence over addition (+) and subtraction (-). Parentheses may be used to group terms. Thus, the following expression yields 12:

(1 + 2) * 4

The next expression yields 9:

1 + 2 * 4

The program in Example 4-1 computes the value of the expression (1 + 2) * 4.

Example 4-1. Simple expression

int main(  )
{
    (1 + 2) * 4;
    return(0);
}

Although we calculate the answer, we don't do anything with it. (This program will generate a " null effect" warning when it's compiled, which indicates that there is a correctly written, but useless, statement in the program.)

If we were constructing a building, think about how confused a worker would be if we said, "Take your wheelbarrow and go back and forth between the truck and the building site."

"Do you want me to carry bricks in the wheelbarrow?"

"No. Just go back and forth."

You need to output the results of your calculations.

The std::cout Output Object

The standard object std::cout is used to output data to the console. We'll learn what a object is later in Chapter 13, but for now all we have to know is that the operator << [2] tells C++ what to output.

So the statement:

std::cout << "Hello World\n";

tells C++ to take the string "Hello World\n" and write it to the console. Multiple << operators may be used together. For example, both the following lines output the same message:

std::cout << "Hello World\n";
std::cout << "Hello " << "World\n";

Expressions can also be output this way, such as:

std::cout << "Half of " << 64 << " is " << (64 / 2) << "\n";

When this is executed, it will write:

Half of 64 is 32

on the console. Note that we had to put a space after the "of" in "Half of". There also is a space on either side of the "is" string. These spaces are needed in the output to separate the numbers from the text. Suppose we didn't put the spaces in, and the code looked like this:

// Problem code
std::cout << "Half of" << 64 << "is" << (64 / 2) << "\n";

At first glance this code looks perfectly normal. There are spaces around each of the numbers. But these spaces are not inside any string, so they will not be output. The result of this code is:

Half of64is32

Omitting needed spaces is a common first-time programming mistake. Remember, only the text inside the quotation marks will be output.

Variables and Storage

C++ allows you to store values in variables. Each variable is identified by a variable name.

Additionally, each variable has a variable type. The type tells C++ how the variable is going to be used and what kind of numbers (real, integer) it can hold.

Names start with a letter followed by any number of letters, digits, or underscores ( _ ).[3] Uppercase is different from lowercase, so the names "sam", "Sam", and "SAM" specify three different variables. To avoid confusion, it is better to use different names for variables and not depend on case differences.

Some C++ programmers use all lowercase variable names. Some names, such as int, while, for, and float , have a special meaning to C++ and are considered reserved words , also called keywords. They cannot be used for variable names.

The following is an example of some variable names:

average            // average of all grades
pi                 // pi to 6 decimal places
number_of_students // number of students in this class

The following are not variable names:

3rd_entry   // Begins with a number
all$done    // Contains a "$"
the end     // Contains a space
int         // Reserved word

Avoid variable names that are similar. For example, the following illustrates a poor choice of variable names:

total       // total number of items in current entry
totals      // total of all entries

This is a much better set of names:

entry_total // total number of items in current entry
all_total   // total of all entries

Variable Declarations

Before you can use a variable in C++, it must be defined in a declaration statement. A variable cannot be used unless it is declared.

A variable declaration serves three purposes:

  • It defines the name of the variable.

  • It defines the type of the variable (integer, real, character, etc.).

  • It gives the programmer a description of the variable.

The declaration of a variable answer can be:

int answer;     // the result of our expression

The keyword int tells C++ that this variable contains an integer value. (Integers are defined below.) The variable name is answer. The semicolon is used to indicate the statement end, and the comment is used to define this variable for the programmer.

The general form of a variable declaration is:

               type  
               name;   // comment 

Type is one of the C++ variable types (int , float, etc.) Name is any valid variable name. The comment explains what the variable is and what it will be used for. Variable declarations come just before the main( ) line at the top of a program. (In Chapter 9 you will see how local variables may be declared elsewhere.)

Integers

One variable type is integer. Integers (also known as whole numbers) have no fractional part or decimal point. Numbers such as 1, 87, and -222 are integers. The number 8.3 is not an integer because it contains a decimal point. The general form of an integer declaration is:

    int  name;   // comment

A calculator with an eight-digit display can only handle numbers between 99,999,999 and -99,999,999. If you try to add 1 to 99,999,999, you will get an overflow error. Computers have similar limits. The limits on integers are implementation- dependent, meaning they change from computer to computer.

Calculators use decimal digits (0-9). Computers use binary digits (0-1) called bits. Eight bits make a byte. The number of bits used to hold an integer varies from machine to machine. Numbers are converted from binary to decimal for printing.

On most machines integers are 32 bits (4 bytes), providing a range of 2,147,483,647 (231- 1) to -2,147,483,648 (-231). Some systems now use newer processors such as the Intel Itanium, which have 64-bit integers, giving you a range of 9223372036854775807 (263-1) to -9223372036854775808 (-263) If you are programming using an older MS-DOS compiler, only 16 bits (2 bytes) are used, so the range is 32,767 (215-1) to -32,768 (-215).

Question 4-1: The following will work on a Unix machine but will fail on an old MS-DOS system.

int zip;      // zip code for current address

......... 

zip = 92126;

Why does this fail? What will be the result when this program is run on an MS-DOS system?

Assignment Statements

Variables are given a value through the use of assignment statements. Before a variable can be used, it must be declared. For example:

int answer;    // Result of a simple computation

The variable may then be used in an assignment statement, such as:

answer = (1 + 2) * 4;

The variable answer on the left side of the equal sign (=) is assigned the value of the expression (1 + 2) * 4 on the right side. The semicolon ends the statement.

When you declare a variable, C++ allocates storage for the variable and puts an unknown value inside it. You can think of the declaration as creating a box to hold the data. When it starts out, it is a mystery box containing an unknown quantity. This is illustrated in Figure 4-1A. The assignment statement computes the value of the expression and drops that value into the box, as shown in Figure 4-1B.

Declaration and assignment statements

Figure 4-1. Declaration and assignment statements

The general form of the assignment statement is:

               variable = expression;

The equal sign (=) is used for assignment, not equality.

In Example 4-2, the variable term is used to store an integer value that is used in two later expressions. Variables, like expressions, can be output using the output operator <<, so we use this operator to check the results.

Example 4-2. tterm/tterm.cpp

#include <iostream>

int term;       // term used in two expressions
int main(  )
{

    term = 3 * 5;
    std::cout << "Twice " << term << " is " << 2*term << "\n";
    std::cout << "Three times " << term << " is " << 3*term << "\n";
    return (0);
}

Floating-Point Numbers

Real numbers are numbers that have a fractional part. Because of the way they are stored internally, real numbers are also known as floating-point numbers. The numbers 5.5, 8.3, and -12.6 are all floating-point numbers. C++ uses the decimal point to distinguish between floating-point numbers and integers, so a number such as 5.0 is a floating-point number while 5 is an integer. Floating-point numbers must contain a decimal point. Numbers such as 3.14159, 0.5, 1.0, and 8.88 are floating-point numbers.

Although it is possible to omit digits before the decimal point and specify a number as .5 instead of 0.5, the extra 0 makes it clear that you are using a floating-point number. A similar rule applies to 12. versus 12.0. Floating-point zero should be written as 0.0.

Additionally, a floating-point number may include an exponent specification of the form e±exp. For example, 1.2e34 is shorthand for 1.2×1034.

The form of a floating-point declaration is:

float variable;   // comment

Again, there is a limit on the range of floating-point numbers the computer can handle. The range varies widely from computer to computer. Floating-point accuracy is discussed further in Chapter 19.

Floating-point numbers may be output using std::cout. For example:

std::cout << "The answer is " << (1.0 / 3.0) << "\n";

Floating-Point Divide Versus Integer Divide

The division operator is special. There is a vast difference between an integer divide and a floating-point divide. In an integer divide, the result is truncated (any fractional part is discarded). For example, the integer divide value of 19/10 is 1.

If either the divisor or the dividend is a floating-point number, a floating-point divide is executed. In this case 19.0/10.0 is 1.9. (19/10.0 and 19.0/10 are also floating-point divides; however, 19.0/10.0 is preferred for clarity.) There are several examples in Table 4-2.

Table 4-2. Expression examples

Expression

Result

Result type

19 / 10

1

Integer

19.0 / 10.0

1.9

Floating point

19.0 / 10

1.9

Floating point (for clarity, do not code like this)

19 / 10.0

1.9

Floating point (for clarity, do not code like this)

C++ allows the assignment of an integer expression to a floating-point variable. It will automatically perform the integer-to-floating-point conversion and then make the assignment. A similar conversion is performed when assigning a floating-point number to an integer variable. Floating-point numbers are truncated when assigned to integer variables.

Example 4-3 demonstrates a variety of floating-point and integer operations.

Example 4-3. float1/float1.cpp

int   integer;  // an integer
float floating; // a floating-point number

int main(  )
{
    floating = 1.0 / 2.0;         // assign "floating" 0.5

    integer = 1 / 3;              // assign integer 0

    floating = (1 / 2) + (1 / 2); // assign floating 0.0

    floating = 3.0 / 2.0;         // assign floating 1.5

    integer = floating;           // assign integer 1

    return (0);
}

Notice that the expression 1/2 is an integer expression resulting in an integer divide and an integer result of 0.

Question 4-2: Why does Example 4-4 print "The value of 1/3 is 0"? What must be done to this program to fix it?

Example 4-4. float2/float2.cpp

#include <iostream>

float answer;  // the result of the divide 

int main(  )
{
    answer = 1/3;
    std::cout << "The value of 1/3 is " << answer << "\n"; 
    return (0);
}

Characters

The type char represents single characters. The form of a character declaration is:

charvariable;   //comment

Characters are enclosed in single quotation marks ('). 'A', 'a' and '!' are character constants. The backslash character (\) is called the escape character . It is used to signal that a special character follows. For example, the character \t can be used to represent the single character "tab." \n is the newline character. It causes the output device to go to the beginning of the next line, similar to a return key on a typewriter. The character \\ is the backslash itself. Finally, characters can be specified by \ nnn, where nnn is the octal code for the character. Table 4-3 summarizes these special characters. For a full list of ASCII character codes, see Appendix A.

Table 4-3. Special characters

Character

Name

Meaning

\b

Backspace

Move the cursor to the left one character.

\f

Form feed

Go to top of a new page.

\n

New line

Go to the next line.

\r

Return

Go to the beginning of the current line.

\t

Tab

Advance to the next tab stop (eight-column boundary).

\'

Apostrophe or single quotation mark

The character '.

\"

Double quote

The character ".

\\

Backslash

The character \.

\ nnn

The character nnn

The character number nnn (octal).

\xNN

The character NN

The character number NN (hexadecimal).

Tip

While characters are enclosed in single quotes ('), a different data type, the string literal, is enclosed in double quotes ("). A good way to remember the difference between these two types of quotes is that single characters are enclosed in single quotes. Strings can have any number of characters (including double quote characters), and they are enclosed in double quotes.

Example 4-5 demonstrates the use of character variables by reversing three characters.

Example 4-5. print3/print3.cpp

#include <iostream>

char char1;     // first character 
char char2;     // second character
char char3;     // third character 

int main(  )
{
    char1 = 'A';
    char2 = 'B';
    char3 = 'C';
    std::cout << char1 << char2 << char3 << " reversed is " <<
            char3 << char2 << char1 << "\n";
    return (0);
}

When executed, this program prints:

ABC reversed is CBA

Wide Characters

The problem with the char data type is that it was designed to hold only a basic character set. This is fine for all the American characters, but what about foreign languages? That where the wchar_t data type comes in. It is used to specify "wide characters" which include not only the basic American characters, but foreign characters as well. A wide character is declared just like a simple character:

char simple;   // A simple character
wchar_t wide;  // A wide character

A simple character is declared by putting the character inside single quotes: 'X'. A wide character uses an "L" prefix to indicate that it is a wide character: L'Ω' .

For example:

simple = 'X';
wide = L'Ω';

Boolean Type

The bool type can hold the value true or false. For example:

bool flag; /* Flag can be on or off */
flag = true;

This type of variable is extremely useful when dealing with logic statements, as described in Chapter 6.

Programming Exercises

Exercise 4-1: Write a program to print your name, Social Security number, and date of birth.

Exercise 4-2: Write a program to print a block E using asterisks (*), where the E is 7 characters high and 5 characters wide.

Exercise 4-3: Write a program to compute the area and perimeter of a rectangle 3 inches wide by 5 inches long. What changes must be made to the program so it works for a rectangle 6.8 inches wide by 2.3 inches long?

Exercise 4-4: Write a program to print "HELLO" in big block letters where each letter is 7 characters high and 5 characters wide.

Answers to Chapter Questions

Answer 4-1: The largest number that can be stored in an int on a Unix machine is 2,147,483,647. When using an old MS-DOS compiler the limit is 32,767. The zip code 92126 is larger than 32,767, so it is mangled and the result is 26,590.

This problem can be fixed by using a long int instead of just an int. The various types of integers are discussed in Chapter 5.

Answer 4-2: The problem concerns the division: 1/3. The number 1 and the number 3 are both integers, so this is an integer divide. Fractions are truncated in an integer divide. The expression should be written as:

answer = 1.0 / 3.0


[1] Technically, the statement causes a set of data declarations to be taken from an include file. Chapter 10 discusses include files.

[2] Technically << is the left shift operator; however, the std::cout object has overloaded this operator and made it the output operator. (See Chapter 16 for a complete discussion of I/O objects and classes and Chapter 18, for a definition of overloading.)

[3] System variable names (names defined the by the standard library) can begin with underscore.

[4] Yes, C does have a bool type. See the C standard: ISO/IEC 9899:1999(E) section 7.16.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required