
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write the first C++ program?
C++ is a high level programming language that is used to develop applications, work with operating systems, and much more. It is an extension of the C language, which combines both procedural programming (available in C) and object oriented programming.
In this article, we will learn step by step the procedure of writing our first C++ program.
Prerequist
For this, make sure that your computer consists of the following two.
Get a C++ Compiler
It is a system that translates the source code (written by a programmer) into machine code (understood by a machine). There are good free C++ compilers available for all major operating system platforms. Download one that suits your platform, or you can use the tutorialspoint.com's online compiler.
Here is the list of the following free compilers available.
- GCC: It is the GNU Compiler Collection, which is a collection of a bunch of different compilers created by GNU. You can download and install this compiler from http://gcc.gnu.org/
- Clang: It is a compiler collection released by the LLVM community. It is available on all platforms. You can download and find install instructions on http://clang.llvm.org/get_started.html
- Visual C++ 2017 Community: This is a free C++ compiler built for Windows by Microsoft. You can download and install this compiler from https://www.visualstudio.com/vs/cplusplus/
Text Editor or IDE
A Text editor or an IDE (Integrated Development Environment) is a tool that is used to write, edit, and compile code.
Here is the list of the following popular and free IDEs available.
Write your C++ program
After setting up your all environment on your computer, follow the following steps to write your first C++ code.
Create a New file
Open your IDE or text editor, create a new file, and save it with the .cpp extension. For example, if your file name is hello, then save it as "hello.cpp".
Write you code
Let's start by printing "Hello world" on our computer screen. For this, you need to write the following code given below into your IDE, and then we will learn how this code works on your computer.
#include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }
Explanation
#include<iostream>
This includes the input/output library, which enables our program to take input from the user and display output on the computer screen.
using namespace std;
It's a C++ statement, which allows you to use all the elements in the std (standard) namespace (standard library like cout, cin, string, etc)
If you don't use a namespace or omit a namespace, then you need to write std:: every time while writing the standard library.
Example
std::cout std::cin
int main()
It's the starting point of a C++ program, it's the main function, from which you will write your code inside curly braces {}. Here, int indicates that this function will return an integer to the operating system.
That's why at the end of the code we write return 0 ( returning an integer value).
cout << "Hello, World!" << endl;
The cout stands for Character Output, it's an object of output stream, which is used to display output on the screen. As already discussed above, it is defined in <iostream> (input/output stream).
<< is an insertion operator that is paired with cout to display the written text.
endl indicates the end of a line, and you will be redirected to the next line.
; It's a semicolon is used to terminate the statement or as an end of this particular command.
return 0 tells that the program has executed successfully.
Compile the Program
After writing the program, we need to translate it to a language that the processor understands, ie, in binary machine code. We will do this by using the compiler we installed in the first step.
For this, you need to open your terminal/cmd and navigate to the location of the hello.cpp file using the cd command. Assuming you installed the GCC, you can use the following command to compile the program.
$ g++ -o hello hello.cpp
This command means that you want the g++ compiler to create an output file, hello, using the source file hello.cpp.
Run the program
Now, after writing and compiling our program. You can run it using;
$ ./hello
Output
You will get the output.
Hello world