Tiny C Compiler

Implementation of programming language C

Tiny C Compiler (often abbreviated as tcc) is a C compiler for x86 and x86-64 platforms developed by Fabrice Bellard. It is written in C and Assembler, and distributed under GNU LGPL.

Implementation features:

  • small size. Compiler executable for x86 is about 100KB, so it can be launched from a floppy disk or rescue disk.
  • fast compilation. The author of the compiler states that tcc compiled Links web-browser 9 times faster than gcc.
  • the language used has several tcc-specific features, for example, memory control and arrays boundary checking. The changes are aimed at improving programs safety.
  • tcc allows to run programs immediately after compilation, using a command line argument. This allows to run programs as shell scripts.

Examples:

Hello, World!:

Example for versions Borland C++ Builder 6, Turbo C++ 1.01, g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25
#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}

Factorial:

Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25

This example uses recursive factorial definition. Note that 13! and larger causes an overflow, so the last lines of the output look like this:

13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184

#include <stdio.h>

unsigned long long factorial(unsigned long long n)
{
    if (n == 0) {
        return 1;
    } else {
        return n * factorial (n - 1);
    }
}

int main(void)
{
    int n;
    for (n = 0; n <= 16; n++) {
        printf("%i! = %lld\n", n, factorial(n));
    }
    return 0;
}

Fibonacci numbers:

Example for versions gcc 3.4.5, gcc 3.4.5 (Objective-C), tcc 0.9.25

This example uses recursive definition of Fibonacci numbers. Note the difference from C++ example: loop counter must be declared outside of the loop, and printf is used for output instead of std::cout.

#include <stdio.h>

int fibonacci(int n)
{
    return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}

int main(void)
{
    int n;
    for (n=1; n<=16; n++)
        printf("%d, ", fibonacci(n));
    printf("...\n");
    return 0;
}

Quadratic equation:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4, tcc 0.9.25

This example works both for C and C++, as well as for Objective-C which is superset of C.

#include <math.h> 
#include <stdio.h>

int main()
{
  int A, B, C;
  double D;
  printf("A = ");
  scanf("%d", &A);
  if (A == 0) {
    printf("Not a quadratic equation.\n");
    return 0;
  }
  
  printf("B = ");
  scanf("%d", &B);
  printf("C = ");
  scanf("%d", &C);

  D = B * B - 4 * A * C;
  if (D == 0) {
    printf("x = %f\n", -B / 2.0 / A);
    return 0;
  }
  
  if (D > 0) {
    printf("x1 = %f\nx2 = %f\n",
           (-B + sqrt(D)) / 2.0 / A, (-B - sqrt(D))/ 2.0 / A);
  } else {
    printf("x1 = (%f, %f)\nx2 = (%f, %f)\n",
           -B / 2.0 / A, sqrt(-D) / 2.0 / A, -B / 2.0 / A, -sqrt(-D) / 2.0 /A);
  }
  return 0;
}

CamelCase:

Example for versions gcc 3.4.5, gcc 4.2.4, tcc 0.9.25

This example is based on character-wise string processing. fgets here reads at most 99 characters into the string, and stops when it finds end-of-string character, so a long line might be split. C doesn’t provide boolean data type, so it has to be simulated using integer variable.

#include <stdio.h>

void main() {
    char text[100],cc[100];
    fgets(text, sizeof text, stdin);
    int i,j=0,lastSpace=1;
    for (i=0; text[i]!='\0'; i++) 
        if (text[i]>='A' && text[i]<='Z' || text[i]>='a' && text[i]<='z')
        {   if (lastSpace>0)
                cc[j] = toupper(text[i]);
            else
                cc[j] = tolower(text[i]);
            j++;
            lastSpace = 0;
        }
        else
            lastSpace = 1;
    cc[j]='\0';
    printf("%s\n",cc);
}