Every C++ program contains one or more functions, one of which must be named main
. The operating system runs a C++ program by calling main
. Here is a simple version of main
that does nothing but return a value to the operating system:
int main()
{
return 0;
}
A function definition has four elements: a return type, a function name, a (possibly empty) parameter list enclosed in parentheses, and a function body. Although main
is special in some ways, we define main
the same way we define any other function.
In this example, main
has an empty list of parameters (shown by the ()
with nothing inside). § 6.2.5 (p. 218) will discuss the other parameter types that we can define for main
.
The main
function is required to have a return type of int
, which is a type that represents integers. The int
type is a built-in type, which means that it is one of the types the language defines.
The final part of a function definition, the function body, is a block of statements starting with an open curly brace and ending with a close curly:
The only statement in this block is a return
, which is a statement that terminates a function. As is the case here, a return
can also send a value back to the function’s caller. When a return
statement includes a value, the value returned must have a type that is compatible with the return type of the function. In this case, the return type of main
is int
and the return value is 0
, which is an int
.
Note the semicolon at the end of the
return
statement. Semicolons mark the end of most statements in C++. They are easy to overlook but, when forgotten, can lead to mysterious compiler error messages.
On most systems, the value returned from main
is a status indicator. A return value of 0
indicates success. A nonzero return has a meaning that is defined by the system. Ordinarily a nonzero return indicates what kind of error occurred.
Types are one of the most fundamental concepts in programming and a concept that we will come back to over and over in this Primer. A type defines both the contents of a data element and the operations that are possible on those data.
The data our programs manipulate are stored in variables and every variable has a type. When the type of a variable named
v
isT
, we often say that “v
has typeT
” or, interchangeably, that “v
is aT
.”
Having written the program, we need to compile it. How you compile a program depends on your operating system and compiler. For details on how your particular compiler works, check the reference manual or ask a knowledgeable colleague.
Many PC-based compilers are run from an integrated development environment (IDE) that bundles the compiler with build and analysis tools. These environments can be a great asset in developing large programs but require a fair bit of time to learn how to use effectively. Learning how to use such environments is well beyond the scope of this book.
Most compilers, including those that come with an IDE, provide a command-line interface. Unless you already know the IDE, you may find it easier to start with the command-line interface. Doing so will let you concentrate on learning C++ first. Moreover, once you understand the language, the IDE is likely to be easier to learn.
Whether you use a command-line interface or an IDE, most compilers expect program source code to be stored in one or more files. Program files are normally referred to as a source files. On most systems, the name of a source file ends with a suffix, which is a period followed by one or more characters. The suffix tells the system that the file is a C++ program. Different compilers use different suffix conventions; the most common include .cc
, .cxx
, .cpp
, .cp
, and .C
.
If we are using a command-line interface, we will typically compile a program in a console window (such as a shell window on a UNIX system or a Command Prompt window on Windows). Assuming that our main
program is in a file named prog1.cc
, we might compile it by using a command such as
$ CC prog1.cc
where CC
names the compiler and $
is the system prompt. The compiler generates an executable file. On a Windows system, that executable file is named prog1.exe
. UNIX compilers tend to put their executables in files named a.out
.
To run an executable on Windows, we supply the executable file name and can omit the .exe
file extension:
$ prog1
On some systems you must specify the file’s location explicitly, even if the file is in the current directory or folder. In such cases, we would write
$ .\prog1
The “.
” followed by a backslash indicates that the file is in the current directory.
To run an executable on UNIX, we use the full file name, including the file extension:
$ a.out
If we need to specify the file’s location, we’d use a “.
” followed by a forward slash to indicate that our executable is in the current directory:
$ ./a.out
The value returned from main
is accessed in a system-dependent manner. On both UNIX and Windows systems, after executing the program, you must issue an appropriate echo
command.
On UNIX systems, we obtain the status by writing
$ echo $?
To see the status on a Windows system, we write
$ echo %ERRORLEVEL%
The command used to run the C++ compiler varies across compilers and operating systems. The most common compilers are the GNU compiler and the Microsoft Visual Studio compilers. By default, the command to run the GNU compiler is
g++
:$ g++ -o prog1 prog1.cc
Here
$
is the system prompt. The-o prog1
is an argument to the compiler and names the file in which to put the executable file. This command generates an executable file namedprog1
orprog1.exe
, depending on the operating system. On UNIX, executable files have no suffix; on Windows, the suffix is.exe
. If the-o prog1
is omitted, the compiler generates an executable nameda.out
on UNIX systems anda.exe
on Windows. (Note: Depending on the release of the GNU compiler you are using, you may need to specify-std=c++0x
to turn on C++ 11 support.)The command to run the Microsoft Visual Studio 2010 compiler is
cl
:C:\Users\me\Programs> cl /EHsc prog1.cpp
Here
C:\Users\me\Programs>
is the system prompt and\Users\me\Programs
is the name of the current directory (aka the current folder). Thecl
command invokes the compiler, and/EHsc
is the compiler option that turns on standard exception handling. The Microsoft compiler automatically generates an executable with a name that corresponds to the first source file name. The executable has the suffix.exe
and the same name as the source file name. In this case, the executable is namedprog1.exe
.Compilers usually include options to generate warnings about problematic constructs. It is usually a good idea to use these options. Our preference is to use
-Wall
with the GNU compiler, and to use/W4
with the Microsoft compilers.For further information consult your compiler’s user’s guide.
Exercises Section 1.1.1
Exercise 1.1: Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the
main
program from page 2.Exercise 1.2: Change the program to return
-1
. A return value of-1
is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator frommain
.