Windows OS

How to Run C++ in Command Prompt – Windows

If you are a Windows user, and you want to code in C++ then you must feel happy to know that it is not mandatory to install heavy compilers on your system to compile your C/C++ codes. All windows systems come with a default command prompt, which is used to run system commands and can also be used to compile C++ code files. The user just needs to install the MinGW GNU compiler which will allow them to compile any C or C++ code through the command prompt.

This guide discusses the process to run C++ code in a Command prompt in the windows system.

Run C++ in Command Prompt – Windows

Running C++ in Command Prompt consists of:

    • Installing GNU Compiler
    • Text Document with C++ Code
    • Compile Code Using Command Prompt

You can skip the first two if you already have a C++ code file and GNU compiler pre-installed.

Installing GNU Compiler

A GNU compiler is used to compile and run codes through the command prompt. To install GNU follow the steps below:

Step 1: Open the GNU compiler link, then download it by clicking on the download button.


Step 2: Now run the MinGW Setup file to begin installing it on the Windows system.


Step 3: Click Continue to confirm the installation directory.


Step 4: Some small files will start downloading and it will take only a few seconds. Click the Continue button once the downloading is finished.


Step 5: Now you will see the Installation Manager window, click on the basic setup, and find mingw32-gcc-g++ package and click on it:


Step 6: Right-click on the highlighted package and select the Mark for Installation option.


Step 7: Then go to the Installation tab and click on Apply Changes:


Select the Apply button to confirm installing the packages.


Step 8: Once the packages are installed, click on the Close button:


 

The GNU installer is only required to be installed once, after that you can compile multiple C and C++ codes.

Create a C++ Code

Now let’s quickly create our C++ code file using the default notepad for that follow the steps below:

Step 1: I have created a separate folder on the desktop for all my C++ codes, you can also do the same. But if you don’t want to do that it is totally up to you. Just go to the desktop and create a New Text Document:


Step 2: Name the Document according to your choice but remember to add a .cpp extension. Here, I named mine as a “program.cpp”:


Step 3: Then paste your C++ code inside the document, I have shared my code below:

#include <iostream>

using namespace std;
int main() {
   char array1[] = { 'W','e','l','c','o','m','e'};
   char array2[] = { 'T','o'};
   char array3[] = { 'L','i','n','u','x','H','i','n','t'};
   
   for (int x = 0; x < 7; x++) {   
       cout <<array1[x];  }
       
       cout <<"\n";
   for (int x = 0; x < 2; x++) {   
       cout<<array2[x];}
   
       cout<<"\n";
   for (int x = 0; x < 9; x++) {   
       cout<<array3[x]; }
   return 0;
   }

 

Once done press Ctrl+S to save the changes.

Compile Code Using Command Prompt

Finally, compile the code using the below-mentioned command:

Step 1: Open the Command Prompt and Run it as an administrator:


Step 2: Then run the below-written command to ensure that the g++ compiler is installed:

$ g++ --version

 

Step 3: Copy the path of your code document directory:


Step 4: Now, in the command prompt, paste the path of the file with a cd command, so that the command prompt can navigate the file easily.

cd <path-of-code>

 
Note: Here my file is inside a codes folder on the Desktop, yours could be different according to the destination of your code.


Step 5: Now compile the file using the g++ command to create an executable file of the code:

g++ <file-name>

 

Step 6: Now just enter “a” command to get the output of the compiled file; a is used to run the executable file which is created in the previous step:

a

 

Conclusion

To run C++ in the command prompt, simply install MingGW, a GNU compiler. Then create a new file using any text editor but remember to use the .cpp extension while saving the file. After that open the command prompt and ensure that the g++ compiler is installed. Then using the g++ command, create an executable file for the original file and run the file by entering “a” in the command prompt to get the output.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.