4 Simple steps to compile and run a C program in command prompt

This article covers 4 simple steps to compile and run a C program in command prompt on Windows. Before you move ahead with the steps, you must complete below 2 prerequisites in order to facilitate smooth execution of these 4 steps.

Let’s get into it. At the end of this article, you will be able to execute your C program successfully using command prompt without any error.

Prerequisites before compiling and running any C program using command prompt:

To run your C program using cmd, here are the prerequisites for Windows.

Prerequisite 1: Download and Install a compiler

To compile any program, you need to have a suitable compiler installed on your development environment. For different programming languages, different compilers are available in the market. You need to choose the compiler based on the programming language you are using. Some of them are free and to use and some are paid.

As an example, we will use gcc compiler to compile C program.

Prerequisite 2: C program with required file extension

You must save your C program with .c extension by using any text editor or IDE to write your program. If you use any IDE, then it will help you to resolve compilation errors easily and also increases coding efficiency.

Below is the sample C program that we will execute using command prompt.

#include
int main()
print("Welcome to TechSupportWhale!");
return 0;
>

Steps to run a C program in command prompt:

Here are the step by step procedure to compile and run c program using command prompt.

Step 1: Open command prompt

Go to Windows search and type cmd. Right-click on the command prompt and “Run as administrator“.

Note: It’s not mandatory to open command prompt in Administrative mode. You can open command prompt with local user rights.

Step 2: Validate availability of gcc compiler

Before compiling your program, first check whether the gcc compiler is installed on your computer or not. To check gcc compiler installation information, type the command command in the command prompt.

gcc –v

If this commands returns gcc compiler version information as below then it means gcc is installed on your machine and it is accessible from command prompt.

If you get “gcc is not recognized as an internal or external command, operable program or batch file.” error message then first resolve this error by setting correct environment variable.

Step 3: Compile your C program

Go to the path where your C program file is located. Type the compilation command in following syntax –

gccprogram_file_name> -o output_executable_file_name>

Type following command and hit enter key.

gccWelcome_Message.c -o welcome_executable

Compile C program using gcc compiler

Executable file created by the compiler

The compiler creates the executable file with welcome_executable name.

Note: If you don’t specify –o argument, then the compiler creates the executable file with a.exe name.

Step 4: Run the executable

This is the final step where you run the newly created executable file (.exe file) using command prompt.

Run an exe in command prompt

Final words:

That’s it. These are the steps you need to follow in order to compile and run a C program using command prompt (cmd). I hope it is very much clear now. If you face any issues, do let us know in the comment section.

Share this: