How to run c program in cmd

 Hello dear readers! this article is going to focus on running c and c++ programs in cmd. When it comes to running a c or a c++ program, we got different options. The most common method is to download a software called "IDE". Some examples of c++ IDEs are code block, dev c++, visual studio,etc. These IDEs have numerous features and tools to enhance our development. But the IDEs are developed for professional developers who have mastery of programming. As a beginner, one should not opt to use IDEs. So, for learning c or c++ programming languages from scratch, we can simply write our code in notepad and run it through CMD. Now let's learn how to run c program in cmd

How to install a c++ compiler to run C program in cmd?

  • First download GCC compiler. It's available on sourceforge.net.
  • Install the GCC compiler on your system.
  • Must copy installation directory during installation.
  • If you already have an IDE installed then you don't need to download anything.

How to check if the path compiler is set?

In order to run code, we need to first check whether a c/c++ compiler is installed on our system or not. Write the following command on cmd.

how to run c program in cmd


If this command is not recognized by cmd then it means either you don't have a c++ compiler or you need to set the system variables.
    

How to set path for compiler to run c program in cmd?

  • Copy the path of the compiler installed on your system. I have dev CPP installed on my system.

how to run c program in cmd

  • Right click on "this pc" in file explorer and then click on properties.

how to run c program in cmd

  • Click on "advance system settings".

how to run c program in cmd

  • Now click "environment variables". 
  • Click on "new" and paste the compiler path that we have already copied here.

how to run c program in cmd

  • Try the "g++ --version" command again and see the result.

how to run c program in cmd
  • Now our cmd is ready to run c and c++ programs.


How to run c program in cmd?

  • Write a c program in notepad or any other text editor. Here is the code of a simple program.
    #include <stdio.h>
    int main() {
       printf("Welcome to programmopedia\n");
       printf("_________________________\n\n");
       printf("Hello, World!\n\n");
       printf("_________________________\n\n");
       return 0;
    }
  • Save the file with ".c" extension.
  • Open cmd and write "g++ first.c -o first.exe" command. Here "first.c" is the source code file name and first.exe is the name of the exe file that the compiler will create. 
  • Here is the output of your program.
how to run c program in cmd

How to run c++ program in cmd?

  • This time write a c++ program in notepad or a text editor of your own choice.
    #include <iostream>
    using namespace std;
    
    int main() {
       cout<<"Welcome to programmopedia"<<endl;
       cout<<"_________________________"<<endl<<endl;
       cout<<"Hello, World!"<<endl<<endl;
       cout<<"_________________________"<<endl<<endl;
       return 0;
    }
  • Now save the program with the ".cpp" extension.
  • Again open cmd and write "g++ program.cpp -o program.exe".
  • Now see the output of your program generated by the compiler in cmd.
how to run c program in cmd

Recommended: 

Now let's conclude the article. we have successfully learned how to run c and c++ program in cmd. I hope you liked the provided information. Thanks for reading and supporting us. 

Post a Comment

0 Comments