How to Compile and Run C program in Terminal?
Last Updated :
07 Apr, 2025
To efficiently compile and run C programs, users typically utilize a compiler or the built-in terminal. The command prompt (CMD) on Windows can be employed to process C programs and generate the desired outputs. To create an executable C program, users must compile their C code using the system terminal.
This article will discuss how to compile and run a C program from the Windows command prompt using MinGW, a user-friendly version of the GCC compiler.
How to Compile and Run C Program in Terminal
Users need to type a few levels of commands into the command prompt or in-build terminal to run the C program without a GUI compiler by following the system configuration. The process of converting C language source code into a machine-code binary system is referred to as "compile" to process "run". Because C is a middle-level programming language and not portable, it must first be translated into machine code using a compiler to be executed and run within the environment. We need to follow the below-mentioned steps to compile and run the C program in the in-build system terminal -
Step 1: Download or Install MinGW officially
First, we must install a C compiler in our system to execute and compile the C code into a binary configuration. For Windows, MinGW is the efficient option for the initial process to implement the programs.
- Install MinGW from the official website > Click on the Downloaded File from the page > Follow the on-screen instructions to prepare the installation process and model functions efficiently.
- Go to the MinGW installation manager window > click on the pop-up button > See the packages required to compile C programs for executable output system.
- Check on the driver boxes that show "mingw32-base" and "mingw-gcc-g++" > Select Continue
- Click on Installation menu to install > Select Apply changes option for packages > Click on Apply button
Step 2: Add the compiler's Path to the system environment via Windows
This is the easiest step to add the compiler's path to the internal system environment variables and compile to run the C program. By this step, we can run the compiler from the command prompt and we won't have to enter the full path environment to the GCC program in the system configuration to compile the C program.
- Press the Windows button from the keyboard > Type environment or environment variables > Click on the search result which shows Edit the system environment variables > Process to execute
- Click on the Environment Variables button > Select the path option under the "System variables" section > Select the Edit button
- Click on New > Type "C:\MinGW\bin" > Click Ok (for 3 times) > Go to Homepage
Step 3: Open the cmd environment or Command Prompt window
Now, open a Command Prompt window and run as administrator to compile and run the C program.
- Press the Windows button from the keyboard > Type cmd or Command Prompt
- Right-click on the Command Prompt from the home screen > Select Run as Administrator option > Run "gcc -- version" at the prompt option to execute
Step 4: Implement the 'cd' Command to run and execute
Now, we need to use the cd command to go to the system directory and compile the code where the pre-structured C program is saved individually.
- Go to "C:\MyPrograms" option > Type "cd C:\MyPrograms" to put the value > Click on the Enter button to execute.
Step 5: Run the 'gcc' command to file management
After implementing the above steps, we can run the GCC command to compile the C program in our system for the further process. We use the structured syntax "gcc filename. c -o filename.exe" which compiles and makes the programs executable in the terminal.
- Remember the File name which contains the C code > Replace "filename. the c" with the internal File name
- Compiled structured program name which ends with ".exe" file > file shows a flag "-o" which specifies the output file config.
Step 6: Run the C program and see the output
It's the final step to compile and run our C program efficiently and see the output in the terminal.
- Type the new program name > Click on the Enter button
C
#include <stdio.h>
int main()
{
int n = 153;
int temp = n;
int p = 0;
while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}
// Condition to check whether the
// value of P equals to user input
// or not.
if (temp == p) {
printf("It is Armstrong No.");
}
else {
printf("It is not an Armstrong No.");
}
return 0;
}
OutputIt is Armstrong No.
Conclusion
C programming language is a middle-level procedural programming language. It offers high-level internal system capabilities like functions and structures in addition to low-level features like memory address access features. The process of compiling and executing a C program on the terminal is simple after implementing the proper steps. Understanding this initial method or steps is essential to writing and developing C programs that will help all the possibilities of design and run them more efficiently in any system.
Also Read
Similar Reads
How to Run C program in Ubuntu Learning to run a C program in Ubuntu is a great first step for anyone starting their programming journey or switching to a Linux-based system. Ubuntu offers a clean and powerful environment to write, compile, and execute C code all from the terminal.In C, this guide will show you how to get started
4 min read
How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
How to Compile, Decompile and Run C# Code in Linux? C# is a modern multi-paradigm programming language developed by Microsoft and released in the year 2000. By multi-paradigm we mean that it includes static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented, and component-oriented programming discip
2 min read
How To Build and Install Go Program? Go (also known as Golang) is an open-source programming language focused on simplicity, reliability, and efficiency, developed at Google in 2007. It's a multipurpose, statically typed, compiled programming language. It's used for server-side (backend) programming, game development, cloud-native appl
2 min read
How to add "graphics.h" C/C++ library to gcc compiler in Linux While trying c graphic programming on Ubuntu, I figured out that graphic.h is not a standard C library and it is not supported by gcc compiler. So I am writing this article to explain the process.If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the imp
2 min read