Horje
How to Compile and Run C program in Terminal?

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.

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.
Install_mingw
  • 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
Installtion02_MinGW
  • Click on Installation menu to install > Select Apply changes option for packages > Click on Apply button
Installation_MinGW

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
System_Environment
  • 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
Path-(1)

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
cmd_1
  • Right-click on the Command Prompt from the home screen > Select Run as Administrator option > Run “gcc — version” at the prompt option to execute
gcc_version

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

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

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

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



Reffered: https://www.geeksforgeeks.org


TechTips

Related
How to Optimize Your Smartphone Camera Settings? How to Optimize Your Smartphone Camera Settings?
How To Track Your Stolen Smartphone? (iPhone or Android) How To Track Your Stolen Smartphone? (iPhone or Android)
How to Run C program in Ubuntu? How to Run C program in Ubuntu?
How to Install Grafana Loki on Ubuntu? How to Install Grafana Loki on Ubuntu?
How to Create a Microsoft Account in Windows 11? How to Create a Microsoft Account in Windows 11?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
28