Sometimes we only want to know whether a few lines of C work. Opening a full IDE for that is slow. Here are four quick ways, from no setup at all to a faster loop on our own machine.
1. Online compiler (no setup)
To check an algorithm or a syntax idea, an online sandbox is enough. We don’t need to install anything.
- OnlineGDB: has a debugger with breakpoints.
- Compiler Explorer (Godbolt): shows the assembly that GCC or Clang generate from our code.
- Replit: good for saving and sharing snippets.
2. One command in the terminal
If GCC or Clang is installed, we save the snippet as test.c and build and run it in one line:
gcc -Wall -Wextra test.c -o test && ./test&&runs./testonly if the compilation succeeded.-Wall -Wextraturn on common warnings, so mistakes show up immediately. For example,int x; return x;giveswarning: 'x' is used uninitialized.
We can even skip the file. gcc -x c - reads C code from standard input:
printf '#include <stdio.h>\nint main(void){printf("hi\\n");return 0;}\n' | gcc -x c - -o t && ./thi
3. Editor shortcuts
If we prefer an editor, a plugin can compile and run for us:
- VS Code: install the Code Runner extension, then press
Ctrl + Alt + N(Cmd + Option + Non Mac) with the.cfile open. - Sublime Text: with a C build system, press
Ctrl + B(Cmd + Bon Mac).
4. Feed input from a file
If the program uses scanf, retyping the values on every run gets tedious. We put them in a file instead. Take this program:
#include <stdio.h>
int main(void) {
int a, b;
scanf("%d %d", &a, &b);
printf("%d + %d = %d\n", a, b, a + b);
return 0;
}Create input.txt containing 3 4, then redirect it into the program with <:
./test < input.txt3 + 4 = 7
Adding -fsanitize=address,undefined -g to the gcc command makes the program report out-of-bounds access and undefined behaviour while it runs. It is handy for short tests.
Summary
- Quick idea, no compiler: use an online sandbox.
- Compiler installed:
gcc -Wall -Wextra test.c -o test && ./test. - Repeated
scanfinput:./test < input.txt.