Ora

How to run c code in terminal?

Published in C++ Programming 5 mins read

To run C code in the terminal, you first need to compile your source code into an executable program using a C compiler, and then you execute that compiled program. This process typically involves a few straightforward steps, ensuring your system is set up correctly to handle C development.

Prerequisites: Installing a C Compiler

Before you can compile and run C code, your system needs a C compiler. The most common compiler suite is GCC (GNU Compiler Collection), which includes a C compiler.

For Windows: Installing MinGW

If you're using Windows, MinGW (Minimalist GNU for Windows) is a popular choice that provides a GCC environment.

  1. Download and Install MinGW:
    • Download the MinGW installer from its official source.
    • Follow the installation wizard to install the necessary components, ensuring the gcc package is selected.
    1. Add MinGW to System PATH:
    • After installation, you must add the path to MinGW's bin directory (e.g., C:\MinGW\bin or C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin) to your system's PATH environment variable. This allows the terminal to find the gcc command regardless of your current directory.
    • To do this: Search for "Environment Variables" in Windows, click "Edit the system environment variables," then "Environment Variables," select "Path" under "System variables," click "Edit," and add the path to your MinGW bin directory.

For Linux and macOS: Installing GCC

  • Linux (Debian/Ubuntu-based): Open your terminal and run:
    sudo apt update
    sudo apt install build-essential

    build-essential typically includes gcc, g++, and make.

  • macOS: Install Xcode Command Line Tools by opening your terminal and running:
    xcode-select --install

    This will provide gcc and other development tools.

Step-by-Step Guide to Compile and Run C Code

Once your C compiler is installed and properly configured in your system's PATH, you can proceed with compiling and running your C programs.

Step 1: Create Your C Source File

First, write your C code and save it with a .c extension. For example, let's create a file named hello.c:

// hello.c
#include <stdio.h>

int main() {
    printf("Hello, C World!\n");
    return 0;
}

Step 2: Open Your Terminal or Command Prompt

  • Windows: Search for "Command Prompt" or "CMD" in the Start menu and open it.
  • Linux/macOS: Open your default terminal application.

Step 3: Navigate to Your C File's Directory

Use the cd (change directory) command to move to the folder where you saved your hello.c file.

Example:
If your file is located at C:\Users\YourUser\Documents\C_Programs on Windows:

cd C:\Users\YourUser\Documents\C_Programs

If your file is located at ~/Documents/C_Programs on Linux/macOS:

cd ~/Documents/C_Programs

Step 4: Compile Your C Code

Now, use the gcc command to compile your source file into an executable program. The -o flag specifies the name of the output executable file.

Command:

gcc hello.c -o hello
  • gcc: The command to invoke the GNU C compiler.
  • hello.c: Your C source code file.
  • -o hello: This flag tells gcc to name the output executable file hello (or hello.exe on Windows). If you omit -o, the default executable name will be a.out (or a.exe on Windows).

If there are no compilation errors, this command will create an executable file named hello (or hello.exe on Windows) in the same directory.

Common gcc Flags

Flag Description
-o <file> Specifies the output file name.
-Wall Enables all common warning messages.
-g Includes debugging information for use with debuggers (like GDB).
-std=c99 Compiles the code according to the C99 standard.
-I<dir> Adds <dir> to the list of directories to be searched for header files.
-L<dir> Adds <dir> to the list of directories to be searched for library files.
-l<library> Links with the specified library (e.g., -lm for the math library).

Step 5: Run the Executable Program

After successful compilation, you can run your program from the terminal.

  • Linux/macOS: Prepend ./ to the executable name to indicate it's in the current directory.
    ./hello
  • Windows: Simply type the executable name (or .\hello.exe if the current directory is not in your PATH).
    hello.exe

    or just

    hello

You should see the output:

Hello, C World!

Example Walkthrough

Let's summarize the process with our hello.c example:

  1. Save Code: Create hello.c with the content:
    #include <stdio.h>
    int main() {
        printf("Hello, C World!\n");
        return 0;
    }
  2. Open Terminal: Launch your command prompt or terminal.
  3. Navigate: Go to the directory where hello.c is saved.
    cd /path/to/your/c/files
  4. Compile: Compile the code using gcc.
    gcc hello.c -o hello

    This creates an executable file named hello.

  5. Run: Execute the compiled program.
    ./hello

    or (on Windows)

    hello.exe

The terminal will then display Hello, C World!.

Common Issues and Tips

  • 'gcc' is not recognized: This usually means the compiler is not installed, or its bin directory is not correctly added to your system's PATH environment variable.
  • Compilation Errors: If gcc reports errors, carefully read the error messages. They usually indicate line numbers and descriptions to help you fix syntax errors or other issues in your C code.
  • Executable Not Found (Linux/macOS): Ensure you preface the executable name with ./ (e.g., ./myprogram) to specify that the program is in the current directory.