Skip to main content
TCC can be invoked from scripts just like shell scripts, allowing you to write executable C programs with a shebang line. This enables C to be used for scripting tasks while maintaining the performance and type safety of compiled code.

Basic scripting

Using shebang

Add a shebang line at the start of your C source file to make it executable:
hello.c
Make the file executable and run it:
Output:
The shebang path may vary depending on where TCC is installed. Common paths are:
  • /usr/local/bin/tcc
  • /usr/bin/tcc
Use which tcc to find the correct path on your system.

Portable shebang

For better portability across different systems, use env:
This searches for tcc in the PATH environment variable.

Command-line arguments

Accessing arguments

Script arguments are passed to the main() function:
script_args.c
Run with arguments:
Output:

Processing arguments

Here’s a practical example that processes file arguments:
file_info.c

TCC options in shebang

Adding compiler options

You can include TCC options in the shebang line:

Linking with libraries

Include library flags in the shebang:
math_script.c

Complex options

For multiple options, quote them as a single argument:
x11_script.c
Some shells limit the length and format of shebang lines. Keep option lists reasonable or use configuration files.

Reading from stdin

TCC can execute C code from standard input:
Output:
This is useful for quick testing or piping generated code:
Output:

Using tcclib

TCC provides tcclib.h, a minimal library header that includes commonly used functions without requiring standard headers:
tcclib.h provides:
  • Standard I/O functions (printf, fprintf, puts, etc.)
  • Memory functions (malloc, free, memcpy, etc.)
  • String functions (strlen, strcmp, strcpy, etc.)
  • Basic file operations (fopen, fclose, fread, etc.)
Using tcclib.h instead of standard headers can slightly improve compilation speed for simple scripts.

Practical examples

File processing script

process_log.c

Text processing

word_count.c
Usage:

System administration

disk_usage.c

Performance considerations

Compilation overhead

While TCC compiles very quickly, there is still a compilation step each time the script runs:
For scripts that run frequently, consider compiling them to avoid the compilation overhead, even though TCC is very fast.

When to use C scripts

C scripting with TCC is ideal for:
  • System programming tasks - Direct access to system calls and low-level APIs
  • Performance-critical scripts - Faster execution than interpreted languages
  • Existing C code reuse - Quickly test or run C code snippets
  • Learning and prototyping - Rapid C development without build systems
C scripting may not be ideal for:
  • Simple text processing - Shell scripts or awk/sed are simpler
  • Complex string manipulation - Higher-level languages have better string support
  • Cross-platform portability - Requires TCC on target system

Custom stdin for scripts

Redirect stdin for the running script:
This is useful for testing scripts that read from stdin:
read_stdin.c
Test with:

Debugging scripts

Enable debug info and bounds checking:
With -g and -b, you get:
  • Clear error messages with file and line numbers
  • Bounds checking for arrays and pointers
  • Stack traces on errors
Use -bt to enable backtrace support and see the full call stack when errors occur.