Tip of the Day: Run a C Program Directly


For this short tip you’ll need tcc, the Tiny C Compiler, which comes with most distributions out there, including Debian and Ubuntu. tcc is a small ANSI C compiler which offers the ability to run the program after compiling it, unlike GCC, which (as far as I know) doesn’t offer this option. To install tcc in Debian and Ubuntu:

In Debian, as root:

apt-get install tcc

In Ubuntu, use:

sudo apt-get install tcc

Now let’s test this. First, create your C source file, e.g.:

#!/usr/bin/tcc -run
#include <stdio.h>

int main ()
{
printf (“Hello, world!n”);
return 0;
}

Now, save this file as example.c or some other name and make it executable:

chmod 755 example.c # or chmod +x example.c

And the next step is just to run it!

./example.c

tcc will compile the source and run it automatically.

Related posts:

  1. How-To: Compile and Install GIMP 2.7.0 in Ubuntu 9.04 Jaunty Jackalope
  2. How-To: Install OpenOffice 3.1 in Debian 5.0 Lenny
  3. How-To: Install Exaile 3.0.1 in Ubuntu 9.04 Jaunty Jackalope
  4. VirtualBox 3.0.2 on Ubuntu 9.04 (i386 or AMD64)
  5. Install Pidgin 2.6.1 in 2 steps for newbie Ubuntu users.

Read more at TuxArena.