This tutorial is part of a Collection: C++ Tutorial
1207
views
03. A simple C++ Program
The first thing everyone learns when learning a new programming language is writing "Hello World!" to the screen. So in this tutorial you will learn how C++ programs work and how to output something to the screen.
##How C++ programs work## In the previous tutorial we saw a simple C++ Hello World program. So now I will explain how this program works. #include <iostream.h> int main() { std::cout<<"Hello World!"<<std::endl; return 0; } Hello World! So now I will explain how C++ programs work. #include <iostream.h> We created a hash sign(#) and after that we said include <iostream.h>. Every line that starts with hash sign(#) is a special line that is interpreted before the compilation of the program itself. With #include we tell the program to include a section of standard C++ code, known as *header iostrem*, that allows to preform standard I/O (Input/Output) operations. Blank lines have no effect on any kind of program. they simply improve the readability of the code. int main() This initiates the declaration of function. Essentially, a function is a group of code statements which are given a name. We will discuss functions in detail in later tutorials. The function main is a special function in C++ programs. It's called when the program runs. The execution of all C++ programs begins with main function regardless of where the function is located in the code. {} The open brace({) indicates the beginning of main function definition, and the closing brace (}) indicates the end. Everything that is inside these braces is the functions body, that defines what happens when main function starts. std::cout<<"Hello World!"<<std::endl; This line is a C++ statement. They are executed in the same order that they appear whitin the function's body. This statement has four parts. First part is std::cout, which identifies the **S**tandard **C**haracter **Out**put device (Usually, this is the computer screen). Second part is the insertion operator (<<), which indicates that what follows is inserted into std::cout. Third part is a sentance whitin quotes ("Hello World!"). Whatever is inserted whithin these quotes will be displayed. The last part is std::endl, which identifies the **S**tandard **End** **L**ine. Notices that both statements in our program end with semicolon(;). This character marks the end of the statement. All C++ statements must end with a semicolon character, otherwise you will have syntax errors. return 0; The return statement means that the program executed without errors. We will learn more about return values in later tutorials. C++ does not have strict rules on how to split statements in diffrent lines. For example insted of int main() { std::cout<<"Hello World!"<<std::endl; return 0; } We could have written the program like this: int main(){ std::cout<<"Hello World!"<<std::endl; return 0; } All in a single line, and this would have had exactly the same meaning as the code written on multiple lines. The seperation between statements is specified with an ending semicolon(;), with the seperation into diffrent lines not mattering at all for this purpose. ##Line spacing## Now, let's add an additional statement to our first program. int main() { std::cout<<"Hello World, "; std::cout<<"I am a C++ programmer!"; return 0; } Hello World, I am a C++ programmer! You can see that these two statements are written on diffrent lines and the statement with Hello World is executed first, then why are the outputs of both statements written on the same line? The answer is rather simple. C++ needs to see that you want something to be written in the second line. In C++ there are two diffrent ways to create a line break. First one is by adding endl at the end of the statement. int main() { std::cout<<"Hello World!"<<std::endl; std::cout<<"I am a C++ programmer!"; return 0; } Hello World! I am a C++ programmer! The second one is by inserting a special character(n) between the quotes. int main() { std::cout<<"Hello World!n"; std::cout<<"I am a C++ programmer!"; return 0; } Hello World! I am a C++ programmer! The only advantage you will have with the character(n) is that you will be able to break line anywhere between quotes. int main() { std::cout<<"This is the first line. There will be couple of empty lines.nnn"; std::cout<<"Here is a line break.nThis is in another line now."; return 0; } This is the first line. There will be couple of empty lines. Here is a line break. This is in another line now. ##Using namespace std## You may seen before that cout in C++ code was used without std::.Cout is part of the standard library, and all the elements in teh standart C++ library are declared within what is called a namespace(namespace std). In order to refer to elements in the std namespace a program shall must qualify each and every use of the library. We can do that by using namespace std. #include <iostream.h> using namespace std; int main() { cout<<"Hello World!"<<endl; return 0; } Hello World!
Sign in to comment