Anatomy of a C Program


Anatomy of a C++ Program

It seems to be a tradition in C++ programming books for the first code example to output to a console window the message Hello World! (shown in Figure 1-1).

click to expand
Figure 1-1: C++ program outputting Hello World! to the screen
Note  

The term console goes back to the days before Windows when the screen did not have menus and toolbars but just text. If you have typed commands using DOS or UNIX, you likely did so in a console window. The text Press any key to continue immediately following Hello World! is not part of the program, but instead is a cue for how to close the console window.

Unfortunately, all too often the Hello World! example is followed quickly by many other program examples without the book or teacher first stopping to explain how the Hello World! program works. The result soon is a confused reader or student who s ready to say Goodbye, Cruel World.

While the Hello World! program looks simple, there actually is a lot going on behind the scenes of this program. Accordingly, we are going to go through the following code for the Hello World! program line by line, though not in top-to-bottom order.

 #include <iostream> using namespace std; int main(void) {  cout << "Hello World!";  return 0; } 
Note  

The code a programmer writes is referred to as source code, which is saved in a file that usually has a .cpp extension, standing for C++.

The main Function

As discussed in the What Is a Programming Language? section, the purpose of C++, or any programming language, is to enable a programmer to write instructions for a computer. Often, a task is too complex for just one instruction. Instead, several related instructions are required.

A function is a group of related instructions, also called statements, which together perform a particular task. The name of the function is how you refer to these related statements. In the Hello World! program, main is the name of a function. A program may have many functions, and in Chapter 9 I will show you how to create and use functions. However, a program must have one main function, and only one main function. The reason is that the main function is the starting point for every C++ program. If there was no main function, the computer would not know where to start the program. If there was more than one main function, the program would not know whether to start at one or the other.

Note  

The main function is preceded by int and followed by void in parentheses. We will cover the meaning of both in Chapter 9.

The Function Body

Each of the related instructions, or statements, which belong to the main function are contained within the body of that function. A function body starts with a left curly brace, {, and ends with a right curly brace , }.

Each statement usually ends with a semicolon. The main function has two statements:

 cout << "Hello World!";  return 0; 

Statements are executed in order, from top to bottom. Don t worry, the term executed doesn t mean the statement is put to death. Rather, it means that the statement is carried out, or executed, by the computer.

cout

The first statement is

 cout << "Hello World!"; 

cout is pronounced C-out. The out refers to the direction in which cout sends a stream of data.

A data stream may flow in one of two directions. One direction is input ”into your program from an outside source such as a file or user keyboard input. The other direction is output ”out from your program to an outside source such as a monitor, printer, or file.

cout concerns the output stream. It sends information to the standard output device. The standard output device usually is your monitor, though it can be something else, such as a printer or a file on your hard drive.

The << following cout is an operator. You likely have used operators before, such as the arithmetic operators +, “, *, and /, for addition, subtraction, multiplication, and division, respectively.

The << operator is known as the stream insertion operator. It inserts the information immediately to its right ”in this example, the text Hello World! into the data stream. The cout object then sends that information to the standard output device ”in this case, the monitor.

Note  

In Chapter 3, you will learn about the counterparts to the cout object and the << operator, the cin object, which concerns the input stream, and the >> operator used with the cin object.

The return 0 Statement

The second and final statement returns a value of zero to the computer s operating system, whether Windows, UNIX, or another. This tells the operating system that the program ended normally. Sometimes programs do not end normally, but instead crash, such as if you run out of memory during the running of the program. The operating system may need to handle this abnormal program termination differently than normal termination. That is why the program tells the operating system that this time it ended normally.

The #include Directive

Your C++ program knows to start at the main function because the main function is part of the core of the C++ language. We certainly did not write any code that told the C++ program to start at main .

Similarly, your C++ program seems to know that the cout object, in conjunction with the stream insertion operator <<, outputs information to the monitor. We did not write any code to have the cout object and the << operator achieve this result.

However, the cout object is not part of the C++ core language. Rather, it is defined elsewhere, in a standard library file . C++ has a number of standard library files, each defining commonly used objects. Outputting information to the monitor certainly is a common task. While you could go to the trouble of writing your own function that outputs information to the screen, a standard library file s implementation of cout saves you the trouble of reinventing the wheel.

While C++ already has implemented the cout object for you in a standard library file, you still have to tell the program to include that standard library file in your application. You do so with the #include directive, followed by the name of the library file. If the library file is a standard library file, as opposed to one you wrote (yes, you can create your own), then the file name is enclosed in angle brackets, < and >.

The cout object is defined in the standard library file iostream . The io in iostream refers to input and output ” stream to a stream of data. To use the cout object, we need to include the iostream standard library file in our application. We do so with the following include directive:

 #include <iostream> 

The include directive is called a preprocessor directive . The preprocessor, together with the compiler and linker, are discussed later in this chapter in the section Translating the Code for the Computer. The preprocessor directive, unlike statements, is not ended by a semicolon.

Namespace

The final statement to be discussed in the Hello World! example is

 using namespace std; 

C++ uses namespaces to organize different names used in programs. Every name used in the iostream standard library file is part of a namespace called std . Consequently, the cout object is really called std::cout. The using namespace std statement avoids the need for putting std:: before every reference to cout, so we can just use cout in our code.




C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net