Using Geekbench on Linux involves downloading the correct package and running it from the terminal, but if you’re here to learn how to run c code in linux terminal, you’re in the right place. Compiling and executing C programs in the Linux terminal is a fundamental skill for any developer. This guide walks you through every step, from installing the compiler to running your first program, with practical examples and troubleshooting tips.
Linux is built for developers. The terminal gives you full control over compiling and running C code. No need for heavy IDEs. Just a text editor and a few commands.
Let’s get started. You’ll be writing and running C code in minutes.
Prerequisites For Running C Code In Linux
Before you can run C code, you need the right tools. Linux distributions come with many packages pre-installed, but the C compiler is often missing.
Check If GCC Is Installed
GCC (GNU Compiler Collection) is the standard compiler for C on Linux. Open your terminal and type:
gcc --version
If you see version information, you’re good. If not, you’ll see an error like “command not found.”
Install GCC On Different Distributions
- Ubuntu/Debian:
sudo apt update && sudo apt install gcc - Fedora/RHEL:
sudo dnf install gcc - Arch Linux:
sudo pacman -S gcc - openSUSE:
sudo zypper install gcc
Once installed, verify again with gcc --version. You should see output like “gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0.”
How To Run C Code In Linux Terminal
Now that GCC is ready, let’s write and run a simple C program. This section covers the complete workflow.
Step 1: Create A C Source File
Use any text editor. Nano is simple for beginners. Vim and VS Code are also popular. Open the terminal and type:
nano hello.c
This opens a blank file. Write your first C program:
#include <stdio.h>
int main() {
printf("Hello, Linux!\n");
return 0;
}
Save the file. In Nano, press Ctrl+O, then Enter, then Ctrl+X to exit.
Step 2: Compile The C Code
Compilation turns your human-readable code into a machine-executable binary. In the terminal, run:
gcc hello.c -o hello
Here’s what each part means:
gcc– the compiler commandhello.c– your source file-o hello– tells GCC to name the output file “hello” (without the -o flag, it defaults to “a.out”)
If there are no errors, the command completes silently. You’ll see a new file in the directory.
Step 3: Run The Compiled Program
To execute the binary, type:
./hello
You should see the output:
Hello, Linux!
That’s it. You’ve successfully run C code in the Linux terminal.
Common Compilation Errors And Fixes
Errors happen. Here’s how to handle the most common ones.
Error: “Fatal Error: Stdio.h: No Such File Or Directory”
This means GCC can’t find standard libraries. Usually, you need to install build-essential (Ubuntu) or glibc-devel (Fedora).
sudo apt install build-essential # Ubuntu/Debian
sudo dnf install glibc-devel # Fedora
Error: “Undefined Reference To ‘Main'”
You forgot to define the main() function. Check that your program has int main() with curly braces.
Error: “Permission Denied” When Running ./Hello
The file might not have execute permissions. Fix it with:
chmod +x hello
./hello
Warning: “Implicit Declaration Of Function”
You’re using a function without including its header. For example, using printf() without #include <stdio.h>.
Compiling Multiple Source Files
Real projects often have multiple .c files. Here’s how to compile them together.
Suppose you have main.c and utils.c. Compile both at once:
gcc main.c utils.c -o myprogram
Or compile each to object files first, then link:
gcc -c main.c -o main.o
gcc -c utils.c -o utils.o
gcc main.o utils.o -o myprogram
This approach is faster for large projects because you only recompile changed files.
Using Make For Automation
For bigger projects, typing compilation commands manually is tedious. Make automates the process.
Create A Simple Makefile
Create a file named Makefile (no extension) with this content:
myprogram: main.o utils.o
gcc main.o utils.o -o myprogram
main.o: main.c
gcc -c main.c -o main.o
utils.o: utils.c
gcc -c utils.c -o utils.o
clean:
rm -f *.o myprogram
Now you can run:
make
Make reads the Makefile and compiles only what’s needed. To clean up, run make clean.
Debugging C Programs In The Terminal
When your code doesn’t work as expected, you need a debugger. GDB (GNU Debugger) is the standard tool.
Compile With Debug Symbols
To use GDB, compile with the -g flag:
gcc -g hello.c -o hello
Start GDB
gdb ./hello
Inside GDB, you can set breakpoints, step through code, and inspect variables. Basic commands:
break main– set a breakpoint at the start of main()run– start the programnext– execute the next lineprint variable_name– display a variable’s valuequit– exit GDB
Running C Code With Input Arguments
Many C programs accept command-line arguments. Here’s how to pass them.
Modify your program to use argc and argv:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
Compile and run with arguments:
gcc args.c -o args
./args hello world 123
Output:
Number of arguments: 4
Argument 0: ./args
Argument 1: hello
Argument 2: world
Argument 3: 123
Using Libraries In C Programs
Sometimes you need external libraries, like math functions. Link them with the -l flag.
Example: Using The Math Library
Create a program that calculates square root:
#include <stdio.h>
#include <math.h>
int main() {
double num = 16.0;
printf("Square root of %.1f is %.1f\n", num, sqrt(num));
return 0;
}
Compile with the math library:
gcc math_demo.c -o math_demo -lm
The -lm flag links the math library. Run it:
./math_demo
Output: Square root of 16.0 is 4.0
Compiling C Code With Optimization
GCC offers optimization levels to make your code run faster or be smaller.
-O0– no optimization (default)-O1– basic optimization-O2– standard optimization (recommended)-O3– aggressive optimization-Os– optimize for size
Example:
gcc -O2 program.c -o program
Higher optimization levels may increase compilation time but produce faster executables.
Handling Warnings Properly
Warnings aren't errors, but they indicate potential issues. Enable all warnings with -Wall:
gcc -Wall program.c -o program
Even better, use -Wextra for additional checks:
gcc -Wall -Wextra program.c -o program
Treat warnings as errors in production code with -Werror:
gcc -Wall -Werror program.c -o program
Running C Code From Different Directories
You don't have to be in the same directory as the source file. Use absolute or relative paths.
gcc /home/user/projects/hello.c -o /home/user/bin/hello
/home/user/bin/hello
Or navigate to the directory first:
cd /home/user/projects
gcc hello.c -o hello
./hello
Using Text Editors For C Development
While Nano works, other editors improve productivity.
Vim
Vim is powerful but has a learning curve. Open a file:
vim program.c
Press i to insert text, Esc to exit insert mode, :wq to save and quit.
VS Code
VS Code with the C/C++ extension provides syntax highlighting and IntelliSense. Open the terminal inside VS Code with Ctrl+`.
Gedit
A simple GUI editor. Install it with sudo apt install gedit.
Common Mistakes Beginners Make
Here are pitfalls to avoid when learning how to run c code in linux terminal.
- Forgetting to save the file – always save before compiling.
- Typing
gccinstead ofgcc– it's GCC, not "gcc" (though both work). - Omitting the
-oflag – you'll get ana.outfile. That's fine, but naming it is clearer. - Running
helloinstead of./hello– Linux doesn't look in the current directory by default. - Not checking for errors – always read compiler output carefully.
Advanced: Using Makefiles With Variables
For more complex projects, use variables in Makefiles to simplify changes.
CC = gcc
CFLAGS = -Wall -Wextra -O2
TARGET = myprogram
OBJS = main.o utils.o
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJS) $(TARGET)
Now you can change the compiler or flags in one place.
Cross-Compiling C Code
Need to compile for a different architecture? Use cross-compilers.
For example, to compile for ARM on an x86 machine:
sudo apt install gcc-arm-linux-gnueabihf
arm-linux-gnueabihf-gcc program.c -o program_arm
This produces a binary that runs on ARM Linux systems.
Running C Code With Valgrind For Memory Checks
Valgrind detects memory leaks and errors. Install it:
sudo apt install valgrind
Run your program through Valgrind:
valgrind ./hello
It reports any memory issues, like uninitialized variables or leaks.
Using Shebang For C Scripts (Not Recommended)
Some people try to run C code like a script with a shebang. This doesn't work natively because C needs compilation. But you can use a trick:
//usr/bin/gcc -o /tmp/$$.out "$0" 2>/dev/null && /tmp/$$.out; rm /tmp/$$.out; exit
This is hacky and not practical for real use. Stick to the standard compile-and-run workflow.
Frequently Asked Questions
Why Do I Get "Command Not Found" When I Type Gcc?
GCC is not installed. Run the install command for your distribution as shown in the prerequisites section.
Can I Run C Code Without Compiling It First?
No. C is a compiled language. You must compile the source code into an executable binary before running it.
What Is The Difference Between Gcc And G++?
GCC compiles C code. G++ compiles C++ code. Use gcc for .c files and g++ for .cpp files.
How Do I Run A C Program With User Input?
Use scanf() in your code. Compile normally, then run the program. It will wait for your input in the terminal.
What Does The -O Flag Do In Gcc?
The -o flag specifies the output file name. Without it, GCC creates a file named a.out.
Final Thoughts On Running C Code In Linux Terminal
Mastering how to run c code in linux terminal opens up endless possibilities. You can write system tools, embedded software, or high-performance applications. The terminal workflow is fast, lightweight, and gives you full control.
Start with simple programs. Experiment with different compiler flags. Use Makefiles for larger projects. Debug with GDB when things go wrong. Over time, these commands will become second nature.
Linux and C are a powerful combination. The terminal is your workspace. Every command you learn makes you a more effective programmer. Keep practicing, and soon you'll compile and run C code without even thinking about it.