Your First C Program

Your First C++ Program

Let’s get our hands dirty with a simple C++ program. Of course, no book on C++ would be complete without including the clich d Hello, World program, so let’s start with that.

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

This short program illustrates some fundamental C++ concepts:

  • The first line uses the directive #include to tell the C++ compiler to copy in the file named iostream at the beginning of the program. Why is this inclusion necessary? A golden rule of C++ is that everything must be declared before it is used, including the output stream named cout used later in the program. (The cout output stream causes output to go to the console.) So why not just declare cout explicitly in this file? Because the iostream file is a separate unit, it can be included in any other program, thus making it easy to reuse the declarations. In a nutshell, this file saves a lot of typing for the programmer.

  • The second line (which begins with using) tells the compiler that the standard C++ library is to be used (hence the word std—an abbreviation of standard). Many different libraries could be used in a single project; the using statement lets us tell the compiler which library we mean to use.

  • The rest of the program is an example of a C++ function. All blocks of code in C++ are called functions—there’s no such thing as a procedure or a subroutine. Each C++ function contains the header (the first line of this program) and the function body (all of the text between the braces { and }). The header shows the return value of the function (in this case int, short for integer), the name of the function (main), and the list of parameters inside round brackets. This example has no parameters, so the round brackets are empty—but the brackets still must be there.

  • All statements in C++ are terminated with a semicolon.

Of the seven lines of text in the example program, only two contain C++ statements: the cout line and the return line. The cout line outputs characters to the console. The syntax for using cout is the word cout followed by a << operator, followed by the items you want to output. (The endl stream manipulation operator inserts a new-line character in the stream.)

You can output many items by using a single cout statement—just separate any extra items with further << operators, as shown here:

cout << "Hello" << ", " << "World" << endl;

Alternatively, you can use several cout statements to give exactly the same effect:

cout << "Hello"; cout << ", "; cout << "World"; cout << endl;

As you might expect, programmers tend to prefer the single-statement version.

The main Function

Why is this example’s only function named main? The simple answer is that the example won’t compile if the function isn’t named main. However, it might be more useful to explain how the language works.

A normal C++ program contains many functions (and also many classes, as discussed in Chapter 2). How does the compiler know which function should be called first? Obviously, the compiler can’t be allowed to just randomly choose a function! The rule is that the compiler will always generate code that looks for a function named main. If you omit the main function, the compiler reports an error and doesn’t create a finished executable program.

start sidebar
Free-Format Languages

The C++ language is free-format, which means that the compiler ignores all spaces, carriage returns, new-line characters, tabs, form feeds, and so on. Collectively, these characters are referred to as white space. The only time the compiler recognizes white space is if it occurs inside a string.

Free-format languages give the programmer great scope for using tab or space indenting as a way of organizing program layout. Statements inside a block of code—such as a for loop or an if statement—are typically indented slightly (often four characters). This indentation helps the programmer’s eye more easily pick out the contents of the block.

The free-format nature of C++ gives rise to one of the most common (and least useful) arguments in the C++ community—how do you indent the braces? Should they be indented with the code, or should they be left hanging at the beginning of the if or the for statement? There is no right or wrong answer to this question (although some hardened C++ developers might disagree), but a consistent use of either style will help make your program readable. As far as the compiler is concerned, your entire program could be written on one line!

end sidebar

So the compiler will expect a function named main. Is that all there is to it? Well, not quite. There are some additional items—such as the return type and parameters being correct—but in the case of main, some of the C++ rules are relaxed. In particular, main can take parameters that represent the command-line arguments, but you can omit them if you don’t want to use the command line.

C++ Keywords and Identifiers

A C++ keyword (also called a reserved word) is a special item of text that the compiler expects to be used in a particular way. The keywords used in the example program are using, namespace, and return. You’re not allowed to use these keywords as variable or function names—the compiler will report an error if you do.

An identifier is any name that the programmer uses to represent variables and functions. An identifier must start with a letter and must contain only letters, numbers, or underscores. The following are legal C++ identifiers:

  • My_variable

  • AReallyLongName

The following are not legal C++ identifiers:


Reason for Being Invalid


Must not start with a number


Must contain only letters, numbers, and underscores


Must not be a reserved word

start sidebar
Compiler Error or Linker Error?

To be absolutely correct, it is the linker that reports the error. You’ll find more on compiler and linker errors later in this chapter.

end sidebar

Outside of these restrictions, any identifier will work. (Oddly enough, the identifier main is not a reserved keyword; you can define a variable named main within the program—but this is not recommended!) Some choices are not recommended. For example:


Reason It’s Not Recommended


Could be confused with the function main.


Too close to the reserved word int.


Just too cryptic!


Underscores at the beginning of names are allowed, but they are not recommended because compilers often use leading underscores when creating internal variable names, and they are also used for variables in system code. To avoid potential naming conflicts, you should not use leading underscores.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208 © 2008-2017.
If you may any questions please contact us: