C3
by Christoffer Lernö
C alternative, trying to stay very close to C while adding conveniences and safety.

C3 is a procedural, low level, language based on C. It tries to be a short step up from C rather than to replace it with a new paradigm or completely new syntax.

The syntax and improvements on C are greatly indebted to the C2 language by Bas van den Berg. It goes substantially further in regards to error handling, macros, generics and strings.

Compared to C, C3 adds:

  • Module system
  • Generic modules
  • Semantic macros
  • Zero overhead error handling
  • Struct subtyping
  • Built-in safe arrays
  • Zero cost simple gradual & opt-in pre/post conditions.
  • High level containers and string handling

It also removes things from C, like type qualifiers, multiple declarations, some type coercions and simplifies the operator precedence.

module test;

struct Foo
{
    int i;
}

func void Foo.next(Foo* foo)
{
    if (foo) foo.i++;
}

macro @do_times(times; @body)
{
    for (int i = 0; i < times; i++) @body();
}

func void test()
{
    Foo foo = { 2 };
    foo.next();
    foo.next();
    // Prints 4
    printf("%d\n", foo.i); 
    @do_times(4)
    {
        foo.next();
    }
    // Prints 8
    printf("%d\n", foo.i); 
}
Information updated 01/31/20
View Comments