Fundamentals of program structure
A C or C++ program is a collection of
variables, function definitions, and function calls. When the program starts, it
executes initialization code and calls a special function,
“main( ).” You put the primary
code for the program here.
As mentioned earlier, a function
definition consists of a return type (which must be specified in C++), a
function name, an argument list in parentheses, and the function code contained
in braces. Here is a sample function definition:
int function() {
// Function code here (this is a comment)
}
The function above has an empty argument
list and a body that contains only a comment.
There can be many sets of braces within a
function definition, but there must always be at least one set surrounding the
function body. Since main( ) is a function, it must follow these
rules. In C++, main( ) always has return type of
int.
C and C++ are free form languages. With
few exceptions, the compiler ignores newlines and white space, so it must have
some way to determine the end of a statement. Statements are delimited by
semicolons.
C comments start with /* and end
with */. They can include newlines. C++ uses C-style comments and has an
additional type of comment: //. The // starts a comment that
terminates with a newline. It is more convenient than /* */ for one-line
comments, and is used extensively in this
book.