No account yet? Register

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

  1. //my first c++ program
  2. #include <iostream>
  3. using namespace std; 
  4. int main(){
  5. cout<<“hello world!”;
  6. return 0;
  7. system(“pause”);
  8. }
  9. /* This ends 
  10. the file */

1.3.3 Include iostream

The lines which begin with hash (#) are used for pre-processors. Pre-processor, is as its name shows the pre-processing information. It works at the start of the program. So here it tells to include standard iostream file before beginning. We will be including some other libraries as well to get some other functionalities. Iostream is used for input output operation in C++. 

1.3.4 using namespace std

Actually without this line you would need to write the “cout” as “std::cout”. So basically we are using “using namespace std;” to tell the program that we are omitting “std::” from now on. 

Also here need to be aware that we are using “;” after all commands we enter in C++. But as you can see not for all the things or lines. 

1.3.5 int main()

Every program in C++ must have this “main()” section. This is actually a function in C++ but it is a standard that you need to write your actual code. We will be working on functions later but main() function must be always in the program. 

On the other hand the “int” at the beginning of the function name is a type of data that we expect at the end of the main() function. In C++ every function has a data type. So here we expect an integer value aftet the main() function is calculated. We will be learning more about the data types in the upcoming sections in this book. 

1.3.4 cout<< “Hello World!”;

So here we get the output from the program with the command “cout”. cout is followed by the output operator “<<”. After “<<” you can write the things you want to be printed on screen. Here we want to show some text, so we need to put that text in between double quotes “”. As we indicated before you need to finish the command with “;”.

1.3.5 return 0;

Here we get the result of the main() function by returning 0. Generally 0 is used as a returned value for main() function, but some other programmers may use some other integer to return. As tradition mostly 0 is used. 

1.3.6 system(“pause”);

This line is to stop the code so that we can see what we did. Actually, we are tricking the system by pausing it. If you did not add this line, you would not be able to see that black screen, but of course it would do the steps we explained above. After doing the things listed in the previous lines it would close the black command screen. But if you add this line, you tell the program to pause. So it will pre-process iostream, come to int function to see what it has and then cout the things you wanted then return 0 as a result of main() function, at the last step, it pauses the screen so we can see the result. 

So here you prepared your very first C++ program and learned the simplest C++ program structure that you need to keep for every program. Now we will start building on top of this base.

NOTE: You may buy this course in paperbook book format from https://www.amazon.com/dp/B0892HQTGS

FavoriteLoadingAdd to favorites
Spread the love

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.