x

C++ Basic program

PREV     NEXT

Now, before learning the C++ language, you need to learn that how to write and compile the programs in C++ language. To understand the program briefly let us have a look at the example:


Let us learn about all the components that C++ program have:

  • #include – It includes the console input output library functions.
  • <iostream.h> – The iostream is a header file that includes the standard input output library functions. It provides cin and cout methods for reading the input and writing the output respectively.
  • <conio.h> – It is also a header file that includes clrscr() and getch() functions.
  • void main – In the void main, main() function is the entry point of program and the void keyword does not have any return value.
  • clrscr() – The clrscr() is used to clear the output screen whenever the program is compiled.
  • cout – The cout method is used to print the data on the console and in this program it will print Welcome to C++ World.
  • getch() – The getch() function is used in the program where it asks for a single character and until you press any key, it will block the screen.

How to compile and execute the program?

  • Now, after writing the program you need to save the program. So, let us have a look how to save the file and compile the program. The following are the steps to save and compile the program:
  • Firstly, open a text editor and add the code as shown above and then save the file as first.cpp
  • After that open the command prompt and then go to the directory where you have saved the program or file.
  • In command prompt type ‘g++ first.cpp’ and press enter to compile the program. If there are any errors in your code firstly, correct them and if there are no errors in your code then you will be taken to next line and will generate the a.out executable file.
  • Now, type ‘a.out’ in command prompt to run your program and after that you will see ‘Welcome to C++ World’ on the window.

PREV     NEXT



Like it? Please Spread the word!