C++ First Program

In the previous lesson, we learned how to setup environment for C++. Now, we will see how to run first C++ program.  It’s quite easy. Let’s begin with launching Turbo C++ and run C++ first program.

Go to Start > Turbo C++

Now, go to File > New, and Open a new C++ project,

Open new C++ project

New C++ program file opens,

New C++ program file opens

Firstly, save it and give any name with extension cpp. Here, we have given the name Study1.cpp,

To save, go to File > Save As

While saving, you can also set the location. After adding the name and location, click Ok to save,

Saving new cpp file

Now, let’s add a demo program

Let’s work on our first C++ program,

#include

The pound sign (#) is for preprocessor and the first word read by the compiler. #include is a directive, which informs the pre-processor to add (include) the iostream standard file.

iostream.h

It is the header file and includes declarations of standard input/output library in C++.

int main()

main() function with return type int.
int is the return value.
Every program in C++ has a main() function. This is where the execution begins.

cout

This is the standard output stream in C++.

return

Terminates the function execution and the return statement returns the control to the calling function.

When you will run the above program, you won’t get an output in Turbo C++, but the program runs correctly. To allow the output to be visible on the screen, remove the return statement and add return type to the main() function as shown below, with getch() method. Also, add <conio.h> header file and getch() for output to be visible in Turbo C++. The MS-DOS compiler use the <conio.h> header file to show console input/output.

getch()  is used to read a character from screen and added just before the program ends. It is a predefined function in <conio.h> header file. To clear the screen, you can also add clrscr(); just after the declaration.

We’re using the <conio.h> header file and getch() to show output on Turbo C++, which is our MS-DOS compiler,

Here’s our C++ program,

Here’s our C++ program on Turbo C++ compiler,

C++ First Program

For output, press Ctrl+F9,

C++ First Program Output

We saw how we can run out first C++ program in Turbo C/C++ compiler.


Recommended Posts

OOPs Overview
C++ Data Types
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading