- [Narrator] A C++ program is basically
a collection of statements and expressions
usually organized into functions and classes.
In this lesson I'll show you the general anatomy
of a C++ program.
Please understand that this is not exhaustive.
There are many other elements that may be involved
in a C++ program.
This is just the basics.
It's just important that you have a general overview
of how these things work.
Here's a sample Hello, World in C++.
Whitespace is any characters that are normally invisible
including new lines, spaces and tabs
and whitespace is mostly ignored in C++.
One notable exception is when a space is necessary
to distinguish between two words or tokens.
For example, the space between the words int and main
is significant and is required.
Extra spaces would be ignored.
Comments are notations in the code
that are ignored by the compiler.
In practice they are stripped out by the preprocessor.
C++ supports two different types of comments.
Single line comments are introduced by two forward slashes.
The comment ends at the end of the line.
In this example, there are two separate comments,
each on a line by itself.
A comment may begin on a line after existing code
without affecting the code itself.
C++ also supports multiline comments.
A multiline comment may span several lines.
The comment begins at the special slash asterisk token
and it ends after the matching asterisk slash token.
Both styles of comments are inherited from the C language.
The multiline comment was part of the original C language
and the single line comment was added later.
The entry point for any C++ program
is always a function called main.
All C++ programs must have a main function.
This is also true for C.
The main function may also have arguments
for parsing the command line.
The first argument is an integer
representing the number of command line arguments.
The second argument is an array of character arrays.
The second argument is sometimes written like this.
It means exactly the same thing.
We'll cover arrays and pointers in more detail
later in this course.
A body of a function is enclosed in curly braces.
The code between the braces
is run whenever the function is called.
The main function must return an integer value.
Zero is commonly returned
to indicate the program X is normally
to indicate an error condition.
A main function would return a one instead of a zero.
Other values may have other meanings
and specific circumstances.
The include directive instructs the preprocessor
to include another file to pass to the compiler.
In this case we're including the C standard I/O header
which supports the puts function
as well as other standard functions
that C++ inherits from C.
It is very common to include header files
at the top of your source code.
In fact, it is rare for a program
not to include header files.
One final note, you'll often see this line
done in other ways.
This is also valid.
I choose to use puts instead of cout because it's simple
so let's take a look at this in the IDE.
You notice I have comments at the top,
I have the include directive
and I have the main function
with its contents inside curly braces.
And when I build and run this,
it displays the message Hello, World.
So as we learned, whitespace is entirely ignored
so I can eliminate a lot of this whitespace.
I can even eliminate that.
And save and build and run
and you notice that it works exactly the same.
I can put all this whitespace back,
I can add a bunch of extra new lines,
I can put extra spaces in.
If I put extra spaces here inside the quotes,
you'll notice that it'll actually
display those extra spaces.
Those are not ignored because they're inside the quotes,
but everything else here is entirely ignored.
I can put in comments, I can use multiline comments.
Or I can put in single line comments like here.
And when I build and run, it works exactly the same.
(upbeat music)