C++ is a general-purpose programming language that is popularly used worldwide. C++ is efficient in terms of both memory and performance that makes it handy in competitive programming. C++ provides features like reusability of code, abstraction, polymorphism, inheritance, etc.
C++ can be run on many platforms like Linux, Windows and macOS. To start coding we need to first set up the environment to compile and run our C++ programs. We can also use online IDEs for the compilation if we don’t want to set up the environment locally. We will go through both the processes in this article.
Many online IDEs provide us with the comfort of running our programs online without setting up the environment locally. Examples are GFG IDE, Codechef IDE etc.
We can choose from other languages also from the left sidebar. We can write our code and then hit the RUN button at the bottom. We can also provide input at the bottom from beside the run button.
Output: We will get our output at bottom of the page like this
In this also we can choose from multiple languages. We can also provide a custom input from the bottom next to the RUN button.
Output: Output will be displayed at the bottom
Output: After compilation we an see output like this
To run C++ code on a local machine we need two main things.
You’ll need a text editor in which you can write your program. Feel free to use any text editor (Notepad++, Sublime, Atom etc.). One important thing that is to be considered here is that the file must be saved with a .cpp extension so that the compiler can consider it as a C++ program.
A compiler is a program that converts the source code of high-level programming language to machine-understandable low-level instructions. We will see how to set up our compiler in Linux.
First of all, open the terminal (Shortcut for Ubuntu: CTRL + ALT + T). Then run these following commands.
sudo apt-get update
Enter the password if prompted
Now to install GCC compiler use below command
sudo apt-get install GCC
Then run this command that will install all libraries required to compile and run the C++ programs
sudo apt-get install build-essential
Now check if the GCC is installed correctly by passing
First we have to open a text editor and write our code
Save the file with name lets say here
first_programme on the desktop with
Then open the terminal (Remember CTRL + ALT + T?)
Then move to the directory where our program file is saved by using
To compile a C++ program with gcc we use below syntax where
filename.cpp is source code file.
output-file-name is the name given to executable file which will be created by the compiler
g++ filename.cpp -o output-file-name
In this case we make a output file with name
On running this command an executable file with name
Output_file_1 will be saved in the same directory where our source code is saved.
To run the program we need to write this command in the terminal window which will show us the output of the program.
Output “Hello World” is printed in the terminal window.
Let’s write a program that takes radius as an input and prints the area of the circle.
Open a text editor and write program
Then save the program with name lets say
second_programme on the desktop with
Go to the program directory in terminal and compile the code
g++ second_programme.cpp -o Output_file_2
Then run the executable file
The terminal will wait for the input of radius, here let us provide 5 and hit enter. We will get output area as 78.5.