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.
To run your C program using cmd, here are the prerequisites for Windows.
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.
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;
>
Here are the step by step procedure to compile and run c program using 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.
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.
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
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.
This is the final step where you run the newly created executable file (.exe file) using command prompt.
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.