C Programming

How to Compile and Run a C Program in Command Prompt

Most Windows operating systems include the command line interpreter program called Command Prompt. It is used to execute instructions that have been entered. Most of those commands make use of scripts and batch files for automating tasks, executing advanced administrative functions, as well as troubleshooting or resolving Windows problems. The Command Prompt can also be utilized for running C programs.

In this tutorial, we will study how to compile as well as run a C program in a Command Prompt. It will help users who don’t want to install separate compilers to execute C programs on their windows systems.

How could Compile and Run a C Program in Command Prompt?

Follow the below-given steps to compile and run your C program in the Windows Command Prompt.

Step 1: Open the start menu of the Windows operating system and then search the command prompt.

Step 2: Then run the following command in the Command Prompt to confirm gcc is installed on your system.

gcc --version

If gcc is not found, you have to download and install a gcc compiler. Follow the guide here if gcc isn’t installed.

Step 3: Now write a C program as well as save it to your computer. I wrote a program and save it in the Downloads directory. A code used by us is given below:

#include <stdio.h>

  int main()

{

printf("LinuxHint Blogs\n");

  return 0;

}

Note: You can write a C code in a text editor by saving a file with .c extension.

Step 4: Change the working path to the location of your C program. You can do this by typing the ‘cd’ command, which changes a current working directory. We must specify the directory where the program is saved.

Step 5: To compile a C program using gcc, follow the below-given syntax:

gcc <sourcefile>.c –o <exe-filename>

For example, in my case, the source file is myfile.c and the output is the name for the exefile.

Following that, an executable file named output.exe will be generated in the same directory where your C file is located.

Step 5: You must run the exe file by calling its name on the Command Prompt and it will show you the output.

In this way, you can run any C program in Command Prompt with ease using the steps mentioned above.

Conclusion

Compiling and running a C program in Command Prompt on Windows is a simple process that can be achieved with just a few commands. After installing a C compiler like gcc, users can create a source code file with a .c extension and then compile it using the “gcc” command followed by the name of the source code file. Once the code is compiled, users can execute their program by running the resulting binary file.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.