Skip to content

/commands/c/compile-and-run-c-program

gcc hello.c -o hello: compile and run a C program

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
C
versions
GCC 9+, Clang 10+, C11/C17/C23
os
linux, macos, windows

gcc hello.c -o hello compiles the C source file hello.c into an executable named hello; ./hello runs it. Without -o, the output is called a.out (a.exe on Windows with MinGW). Add -Wall -Wextra to see warnings and -std=c17 (or another standard) to pin the language version. clang accepts the same options.

Compile and run

The example uses Clang 20 from the ziglang package on PyPI (Zig ships a complete Clang), run through uv; cc below stands for it. On your machine, write gcc or clang instead:

bash
cc() { uv run -q --no-project --python 3.13 --with ziglang==0.15.1 python -m ziglang cc -fno-color-diagnostics "$@"; }
cat > hello.c <<'EOF'
#include <stdio.h>

int main(int argc, char *argv[]) {
    const char *name = argc > 1 ? argv[1] : "world";
    printf("Hello, %s! (%d argument%s)\n", name, argc - 1, argc == 2 ? "" : "s");
    return 0;
}
EOF
cc -std=c17 -Wall -Wextra -O2 hello.c -o hello
./hello
./hello C
echo "exit status: $?"

Output

Hello, world! (0 arguments)
Hello, C! (1 argument)
exit status: 0

The value main returns becomes the exit status of the program, which the shell shows in $?.

Common options

OptionEffect
-o NAMEName of the output file (default a.out, or a.exe on MinGW)
-Wall -WextraEnable the common warnings; see the dedicated page
-std=c17 / -std=c11 / -std=c23Language standard; -std=gnu17 adds GNU extensions
-O0 / -O2 / -O3No optimization (default), the usual release level, more aggressive
-gDebug information for gdb or lldb
-cCompile to an object file (.o) without linking
-lmLink the math library (sqrt, pow) on Linux with glibc
-fsanitize=address,undefinedDetect memory and undefined-behaviour errors at run time (GCC and Clang)

Compiler errors

Errors stop the build and name the file, line and column. A missing #include shows up as an implicit declaration of function error (or warning on old compilers); a misspelt variable as use of undeclared identifier; a forgotten semicolon as expected ';' before. Fix the first error first: later ones are often consequences of it.

Pitfalls

  • gcc: command not found. Install a compiler: build-essential on Debian/Ubuntu, xcode-select --install on macOS (clang), MSYS2/MinGW-w64 or Visual Studio Build Tools on Windows.
  • Math functions. With glibc, code that uses sqrt or pow may need -lm at the end of the command; otherwise linking fails with an undefined reference.
  • Running without ./. hello alone gives command not found on Linux and macOS.
  • Crashes at run time. A program that compiles can still crash with a segmentation fault; build with -g -fsanitize=address to find where.
  • Several source files. Pass them all, or compile each with -c and link: see compiling multiple C files.

Confirm it worked

ls -l hello shows a new executable file, and ./hello prints the expected output. gcc --version (or clang --version) shows which compiler built it.

FAQ

What is a.out?

The default output name of gcc and clang on Unix-like systems when you do not pass -o. On Windows with MinGW the default is a.exe. Pass -o NAME to choose the name.

Why do I need ./ in front of the program name?

On Linux and macOS the current directory is not in PATH, so the shell does not find hello on its own. ./hello runs the file in the current directory. In cmd.exe and PowerShell, hello.exe or .\hello.exe works.

gcc or clang?

For these commands they are interchangeable: clang accepts the same basic options (-o, -c, -Wall, -std, -O2, -g). macOS ships clang, and its gcc command is an alias for clang.

Sources