C++ for Beginners: Chapter-2 (Writing your First Program in C++)

Note:The programs you will find in this series may differ from other sources because every IDE uses different template for C++

Welcome back to 'C++ for Beginners'!Now that we have set up our working environment (IDE), we are all set to finally starting writing our program and understand each line of the codes.For instructions on setting up the IDE, read our first chapter of the series here: Link: C++ for beginners chapter-1
Open TurboC/C++ (Our compiler) and let's start writing the codes!Create a new file by clicking File>>New.Now copy the following text directly into the compiler→

#include<iostream.h> void main() { cout<<"Hello world"; } 

Now Save the program from File>>Save and then specifying the directory and a file name. In order to make sure that there are no errors in our program, we need to Compile the codes.
Compile the code using Compile>>Compile. Before we Run ANY program, it is advisable to save and compile them to avoid any loss of data during the execution of the codes.
If your mouse or the cursor seizes to work at this point of time, then don't worry. Simply follow the keyboard shortcuts given at the bottom of the page to save, compile and run.
If you see any errors, then check whether you have written each and every word and symbol like I have written above and rectify it.Now Run the program by clicking Run>>Run.
Understanding the above code:


The above line calls a header file. In this case, it is calling the header file iostream.h.
A header file is basically a file which contains multiple line of codes to activate certain functions in a program. These files are very helpful in C++ because we do not have to write a piece of code again and again in a single program.# is called as 'preprocessor' and it tells the compiler to first include the given header files before executing the program.include tells the compiler to include the file in the program.And the last is to specify the header file name.

void main()

The execution of the program starts from the function main(). We will discuss about functions later on in the series. For now you should only know that a function is a piece of code which is used to perform a specific task. All our program comes in the main function. All the functions have open and closing parenthesis, the syntax of a function is shown above.
void is basically telling the compiler that the function is not returning any value. Like in English, void means null here as well.

cout<<"Hello world";  

cout — As we mentioned that the header file contains a variety of functions for using later on in our code, cout is one of them. It is used to display anything in the output. It will display all the words, numbers and symbols which come between the two double inverted comas » » as it is.In the above example it will display the words Hello world.
That's it for now!This ends our chapter number 2. In the next chapter we will talk about Variables and their Declarations.Until then, regards and goodbye!
If you like the article please share it and help us grow.

Понравилась статья? Поделиться с друзьями:
Добавить комментарий