How to Compile and Run C program in Terminal? - GeeksforGeeks (2024)

Last Updated : 30 Jul, 2024

Comments

Improve

To compile and run the C programs efficiently users often use a compiler or system in-build terminal. In that case, the well-known system terminal called CMD or command prompt can also be used to process the C programs with required outputs. To create an executable C program on the Windows system configuration, users must compile any code in the C programming language they have made or obtained to compile and run in the system terminal.

In this article, we’ll explore how to compile and run a C program from the Windows command prompt using the MinGW system environment, an easy-to-install version of the GCC compiler to execute correctly.

Table of Content

  • How to Compile and Run C Program in Terminal
    • Step 1: Download or Install MinGW officially
    • Step 2: Add the compiler’s Path to the system environment via Windows
    • Step 3: Open the cmd environment or Command Prompt window
    • Step 4: Implement the ‘cd’ Command to run and execute
    • Step 5: Run the ‘gcc’ command to file management
    • Step 6: Run the C program and see the output
  • Conclusion
  • How to Compile and Run C Program in Terminal – FAQs

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 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.

How to Compile and Run C program in Terminal? - GeeksforGeeks (1)

  • 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

How to Compile and Run C program in Terminal? - GeeksforGeeks (2)

  • Click on Installation menu to install > Select Apply changes option for packages > Click on Apply button

How to Compile and Run C program in Terminal? - GeeksforGeeks (3)

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

How to Compile and Run C program in Terminal? - GeeksforGeeks (4)

  • 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

How to Compile and Run C program in Terminal? - GeeksforGeeks (5)

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

How to Compile and Run C program in Terminal? - GeeksforGeeks (6)

  • Right-click on the Command Prompt from the home screen > Select Run as Administrator option > Run “gcc — version” at the prompt option to execute

How to Compile and Run C program in Terminal? - GeeksforGeeks (7)

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.

How to Compile and Run C program in Terminal? - GeeksforGeeks (8)

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.

How to Compile and Run C program in Terminal? - GeeksforGeeks (9)

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;}

Output

It is Armstrong No.

How to Compile and Run C program in Terminal? - GeeksforGeeks (10)

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

  • How To Compile And Run a C/C++ Code In Linux
  • How to Compile a C++ Program Using GCC
  • How to Compile and Run C/C++/Java Programs in Linux

How to Compile and Run C Program in Terminal – FAQs

How to compile and run a C program in the terminal quickly?

After creating a C program, user can locate the internal file to any folder in the system and entering the C source code in any text editor by formatting the texts. Follow the below steps to compile and run C program quickly in the terminal or command prompt –

  • Go to cd command > Type gcc myfile.c -o hello to process > Compile it > Run

How to open a terminal in our system?

Users can use the in-build system terminal or command prompt to compile and run any programming by logical way. Follow the below steps to open a terminal to implement any operation –

  • Go to Start or Run window > type cmd or command prompt > Click on Run as administrator

How to set and execute any program in cmd?

Command prompt helps the users to set their programs and execute properly to run in the system environment. By following the process all can compile and run any program in cmd –

  • Go to Start > Click on Run button > type cmd > Copy the program file > Paste the file name in cd command > Execute and run the program > See output.


arponkumarchowdhury34

How to Compile and Run C program in Terminal? - GeeksforGeeks (12)

Improve

Previous Article

cpp command in Linux with Examples

Next Article

How to Optimize Your Smartphone Camera Settings?

Please Login to comment...

How to Compile and Run C program in Terminal? - GeeksforGeeks (2024)

References

Top Articles
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6219

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.