24 Sep 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,
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,
Now, let’s add a demo program
Let’s work on our first C++ program,
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> using namespace std; int main() { cout<<"Studyopedia C++ Tutorial"; return 0; } |
#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,
1 2 3 4 5 6 7 8 9 10 |
#include <iostream.h> #include <conio.h> void main() { cout<<”studyopedia c++ tutorial”; getch(); } |
Here’s our C++ program on Turbo C++ compiler,
For output, press Ctrl+F9,
We saw how we can run out first C++ program in Turbo C/C++ compiler.
Recommended Posts
No Comments