Stat Mania
  • C Programming
  • All Courses
  1. Getting Started
  2. How to Test a Small C Snippet Quickly
  • Course Home
  • Getting Started
    • C's "Hello, World!" Explained Line by Line (for R & Python Users)
    • How to Test a Small C Snippet Quickly
  • Variables and Data Types
    • Declaring Multiple Variables in One Line in C
    • float vs double in C: Precision, Memory, and When to Use Which
  • Operators and Input
    • Pre-Increment vs Post-Increment in C: ++i vs i++
    • Checking User Input in C: Is It Really a Number?
  • Making Decisions
    • Nested if in C, Explained with Real-Life Examples
    • Shortcut Ways to Write if in C: the Ternary Operator and More
    • The switch Statement in C: Syntax, break, Fall-Through and Examples
  • Loops
    • The do-while Loop in C: Run First, Check After
    • Why Use break in a while Loop If the Condition Already Stops It?
    • Printing a Comma-Separated List in C (No Trailing Comma, Clean Line Breaks)
    • 5 while Loop Practice Problems in C (with Hints and Solutions)
    • 5 for Loop Practice Problems in C (with Hints and Solutions)
    • 5 Nested for Loop Practice Problems in C (with Hints and Solutions)
  • Arrays and Structs
    • How to Save Loop Results in an Array in C
    • Structs in C: Grouping Related Data (with Python and R Comparisons)
  • Random Numbers
    • srand(time(NULL)) in C: Why Random Numbers Repeat and How to Seed Them
    • Tossing a Die in C Until We Get a 6

On this page

  • 1. Online compiler (no setup)
  • 2. One command in the terminal
  • 3. Editor shortcuts
  • 4. Feed input from a file
  • Summary
  • References
  1. Getting Started
  2. How to Test a Small C Snippet Quickly

How to Test a Small C Snippet Quickly

The fastest ways to try a few lines of C: online compilers, a one-line gcc build-and-run command, editor shortcuts, and feeding input from a file.
Reading 5 min

By the end of this lesson you will

  • Build and run a C file with one gcc command
  • Choose a fast way to try small snippets

Resources

  • GCC manual
  • OnlineGDB C compiler

A sketch of a person at a computer whose terminal runs gcc test.c and prints hi, beside a hexagonal C logo with scraps of C code floating around.

Write a few lines of C, then build and run them with one gcc command.

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 ./test only if the compilation succeeded.
  • -Wall -Wextra turn on common warnings, so mistakes show up immediately. For example, int x; return x; gives warning: '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 && ./t
hi

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 + N on Mac) with the .c file open.
  • Sublime Text: with a C build system, press Ctrl + B (Cmd + B on 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.txt
3 + 4 = 7
TipCatch memory bugs too

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 scanf input: ./test < input.txt.

References

  • Stack Overflow: how to test a C or C++ snippet quickly
  • Quora: testing a C or C++ program without entering inputs manually
  • gcc file.c -o out && ./out builds and runs in one line.
  • Online compilers need no setup for tiny snippets.
  • Add -Wall -Wextra to catch mistakes early.

Redirect. < makes a program read its input from a file instead of the keyboard.

Back to top
Stat Mania

Free, hands-on statistics and programming courses that run right in your browser.

Courses

All courses R Programming Statistics Probability

Resources

Practice quizzes Tools & games Quarto docs Course kit on GitHub

Company

Home About Contact Privacy policy Terms of use
© Stat Mania. All rights reserved.
C’s “Hello, World!” Explained Line by Line (for R & Python Users)
Variables and Data Types