![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |
![]() | Section 2: A First Program |
Now that you've successfully compiled your program, let's take a look at each line of code so that you have a general understanding of how the program works. It will probably look confusing if you've never seen C++ syntax before, but that's completely natural.
Here's the program again:
//include this file for cout #include <iostream.h> int main() { //print out the text string, "Hello, World!" cout << "Hello, World!" << endl; return 0; }
Let's take a look at each line of code that makes up hello.C
.
//include this file for cout
This line is a comment line. The//
indicates that everything following it should be ignored by the compiler. This allows you to add English explanations to what might otherwise be confusing code. You have the freedom to comment your code as much as you like -- some programmers write code with no comments at all; others write several lines of comments for each line of C++ code. It's all up to you. Keep in mind, though, that if anyone else will ever read your code, you'll probably want to add comments. Even if you are the only one who will ever read your code, you should add comments. It sounds implausible, but programmers often don't understand code they've written weeks ago!more on comments
#include <iostream.h>
This line is read "pound include i-o-stream dot h". The effect of this line is to essentially "copy and paste" the entire fileiostream.h
into your own file at this line. So you can think of this syntax as replacing the line#include <iostream.h>
with the contents of the fileiostream.h
.#include
is known as a preprocessor directive, which will be covered much later.
Where is the fileiostream.h
?
This file is located somewhere in your include path. The include path indicates the directories on your computer in which to search for a file, if the file is not located in the current directory.
Why do I need to includeiostream.h
?
In this case,iostream.h
is a file containing code for input/output operations. You need to includeiostream.h
so that the compiler knows about the wordcout
, which appears a couple of lines below.more on preprocessor directives
int main() {
Every C++ program must have what is known as amain
function. When you run the program, the program will go through every line of code in themain
function and execute it. If yourmain
is empty, then your program will do nothing.
There are essentially four parts to a function definition. They are the return type, the function name, the parameter list, and the function body, in that order. In this case:For now, the important thing to remember is that the function body is the part enclosed in
- return type:
int
- function name:
main
- parameter list:
()
- function body:
{ ... }
{ ... }
("curly braces"). The{
indicates the beginning of the function, and the}
indicates the end of the function. The function body is the stuff in between.more on functions
//print out the text string, "Hello, World!"
Another comment line. Remember, the compiler ignores anything
following //
(up until the end of the line), so you can
say whatever you want on these lines.
cout << "Hello, World!" << endl;
This is the line that prints out the text string, "Hello, World!". For now, don't worry about howcout
works, just know how to use it. You can print out any series of text strings by separating them with<<
. So, instead of sayingcout << "Hello, World!" << endl;
, you could saycout << "Hello, " << "World" << "!" << endl;
. Theendl
simply adds a carriage return (stands for "end-line").more on streams
return 0;
This line is necessary because the return type ofmain
isint
(see above). We'll talk more about functions and return types later, but for now understand that because the function's return type isint
, the function must return anint
(integer). To return 0 (which is an integer, we simply writereturn 0;
.more on return values
![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |