Writing and Compiling a Simple C++ Program

writing compiling program techhyme

A digital computer is a useful tool for solving a great variety of problems. A solution to a problem is called an algorithm; it describes the sequence of steps to be performed for the problem to be solved.

Below is the simple “Hello WorldProgram.

#include <iostream.h>
int main (void)
{
cout << “Hello World\n”;
}

#include <iostream.h>

This line uses the preprocessor directive #include to include the contents of the header file iostream.h in the program. Iostream.h is a standard C++ header file and contains definitions for input and output.

int main (void)

This line defines a function called main. A function may have zero or more parameters; these always appear after the function name, between a pair of brackets. The word void appearing between the brackets indicates that main has no parameters.

A function may also have a return type; this always appears before the function name. The return type for main is int (i.e., an integer number). All C++ programs must have exactly one main function. Program execution always begins from main.

{ }

The above brace marks the beginning of the body of main and the end of the body of the main.

cout << “Hello World\n”;

This line is a statement. A statement is a computation step which may produce a value. The end of a statement is always marked with a semicolon (;). This statement causes the string “Hello World\n” to be sent to the cout output stream. A string is any sequence of characters enclosed in double-quotes. The last character in this string (\n) is a newline character which is similar to a carriage return on a type writer. A stream is an object which performs input or output.

Cout is the standard output stream in C++ (standard output usually means your computer monitor screen). The symbol << is an output operator which takes an output stream as its left operand and an expression as its right operand, and causes the value of the latter to be sent to the former. In this case, the effect is that the string “Hello World\n” is sent to cout, causing it to be printed on the computer monitor screen.

Compiling a Simple C++ Program

User input appears in bold and system response in plain. The UNIX command line prompt appears as a dollar symbol ($).

$ CC hello.cc
$ a.out
Hello World
$

CC hello.cc

The command for invoking the AT&T C++ translator in a UNIX environment is CC. The argument to this command (hello.cc) is the name of the file which contains the program.

As a convention, the file name should end in .c, .C, or .cc. (This ending may be different in other systems.)

a.out

The result of compilation is an executable file which is by default named a.out. To run the program, we just use a.out as a command.

Hello World

This is the output produced by the program.

The CC command accepts a variety of useful options. An option appears as -name, where name is the name of the option (usually a single letter). Some options take arguments.

For example, the output option (-o) allows you to specify a name for the executable file produced by the compiler instead of a.out.

$ CC hello.cc -o hello
$ hello
Hello World
$

Although the actual command may be different depending on the make of the compiler, a similar compilation procedure is used under MS-DOS. Windows based C++ compilers offer a user-friendly environment where compilation is as simple as choosing a menu command. The naming convention under MS-DOS and Windows is that C++ source file names should end in .cpp.

You may also like:

Related Posts