Categories
Uncategorized

Scripting with C

Normally scripting refers to the interpreted languages. You write a script that gets interpreted during runtime by interpreters. Python, PHP and Perl are some of the prominent scripting languages. On the other hand, with compiled languages the source code is first compiled then linked to related libraries and finally built into executables or other runtime components. C is a very good and well known example of compiled languages.

Interpreted languages are easy to debug because of their interpreted nature because code execution stops only when it encounters a bug. On the other side, with compiled languages the entire source is compiled before execution. Again, the same problem goes with prototyping. You make a change and go through the usual compile-link-build cycle over and over to reflect the changes. But with interpreted languages, changes in your scripts are immediately reflected in the next run.

C being a compiled language hardly supports scripting. Note that word, yeah, “hardly”, because we can script with C. How? Read on.

C source codes need to be compiled using compilers. You will find a good enough numbers of them to confuse you to decide which one to use. The GCC (GNU C Compiler) is one of the most popular. But the magic I am describing can be performed with a GCC like compiler that is somthing more than cool. The compiler is named TinyCC (aka TCC). It’s a hyper fast compiler. You can use it like a C interpreter to execute C codes on the fly like a script.

EXAMPLE:

How to Run?:

  1. Save the file as “hello.c”.
  2. Make sure TCC is added to your path.
  3. Run the command line tool and navigate it to the directory where hello.c is saved.
  4. Invoke TCC by typing: tcc -run hello.c.

Output: Hello World!

#!/usr/local/bin/tcc -run: Yeah, you can use it like a shell script as well (on linux).

The TCC compiler is so fast that you don’t even need to create makefiles for large projects. TCC has it’s own linker and assembler. TCC not only supports ANSI C, but also most of the new ISO C99 standard and many GNUC extensions including inline assembly. TCC can also automatically generate memory and bound checks while allowing all C pointers operations. TCC can do these checks even if non patched libraries are used. With libtcc, you can use TCC as a backend for dynamic code generation.

TCC mainly supports the i386 target on Linux and Windows. There are alpha ports for the ARM (arm-tcc) and the TMS320C67xx targets (c67-tcc). More information about the ARM port is available at http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html. But even after all these features, TCC is really small.

I am really enjoying my time with this “tiny” yet great tool 🙂

I would recommend all C programmers to give it a try 😀

Download Link: TCC Website

2 replies on “Scripting with C”

You can not believe how long ive been looking for something like this. Browsed through 6 pages of Yahoo results couldnt find diddly squat. Quick search on bing. There was this…. Really gotta start using that more often

Comments are closed.