Small shell helper to compile and run C programs quickly from the terminal.
Instead of running gcc manually every time, this function compiles a .c file and immediately executes the resulting binary.
c program.cThis will:
- Compile the file with
gcc - Enable useful warnings (
-Wall -Wextra) - Include debug symbols (
-g) - Run the compiled binary
Example:
c hello.cYou need:
- gcc
- a POSIX shell (bash / zsh)
Install gcc if you don't have it.
sudo apt install build-essentialsudo pacman -S gccsudo dnf install gccCopy the function into your shell config file.
For bash:
~/.bashrcFor zsh:
~/.zshrcThen paste:
c() {
local file="$1"
if [[ -z "$file" ]]; then
echo "Usage: c file.c"
return 1
fi
if [[ ! -f "$file" ]]; then
echo "File not found"
return 1
fi
local output="${file%.c}"
gcc "$file" -Wall -Wextra -lm -g -o "$output" && "./$output"
}Reload your shell:
source ~/.zshrcor
source ~/.bashrcCreate a script:
sudo nano /usr/local/bin/cPaste:
#!/usr/bin/env bash
file="$1"
[ -z "$file" ] && echo "Usage: c file.c" && exit 1
[ ! -f "$file" ] && echo "File not found" && exit 1
output="${file%.c}"
gcc "$file" -Wall -Wextra -g -o "$output" && "./$output"Make it executable:
sudo chmod +x /usr/local/bin/cNow you can run:
c program.cfrom anywhere.
Just a tiny utility to remove friction when experimenting with small C programs.
Compile → run → repeat.
Made with <3 by URDev