C3 is a C-like language striving to be an evolution of C, rather than a completely new language. As an alternative in the C/C++ niche it aims to be fast and close to the metal.
The manual for C3 can be found at www.c3-lang.org.
Thanks to full ABI compatibility with C, it's possible to mix C and C3 in the same project with no effort. As a demonstration, vkQuake was compiled with a small portion of the code converted to C3 and compiled with the c3c compiler. (The fork can be found at https://github.com/c3lang/vkQuake)
- Procedural "get things done"-type of language.
- Try to stay close to C - only change what's really necessary.
- C ABI compatibility and excellent C integration.
- Learning C3 should be easy for a C programmer.
- Data is inert.
- Avoid "big ideas" & the "more is better" fallacy.
- Introduce some higher level conveniences where the value is great.
C3 owes its inspiration to the C2 language: to iterate on top of C without trying to be a whole new language.
The following code shows generic modules (more examples can be found at http://www.c3-lang.org/examples/).
module stack <Type>;
// Above: the parameterized type is applied to the entire module.
import std::mem;
struct Stack
{
usize capacity;
usize size;
Type* elems;
}
// The type methods offers dot syntax calls,
// so this function can either be called
// Stack.push(&my_stack, ...) or
// my_stack.push(...)
fn void Stack.push(Stack* this, Type element)
{
if (this.capacity == this.size)
{
this.capacity *= 2;
this.elems = mem::realloc(this.elems, $sizeof(Type) * this.capacity);
}
this.elems[this.size++] = element;
}
fn Type Stack.pop(Stack* this)
{
assert(this.size > 0);
return this.elems[--this.size];
}
fn bool Stack.empty(Stack* this)
{
return !this.size;
}
Testing it out:
import stack;
// Define our new types, the first will implicitly create
// a complete copy of the entire Stack module with "Type" set to "int"
define IntStack = Stack<int>;
// The second creates another copy with "Type" set to "double"
define DoubleStack = Stack<double>;
// If we had added "define IntStack2 = Stack<int>"
// no additional copy would have been made (since we already
// have an parameterization of Stack<int>) so it would
// be same as declaring IntStack2 an alias of IntStack
// Importing an external C function is straightforward
// here is an example of importing libc's printf:
extern fn int printf(char* format, ...);
fn void test()
{
IntStack stack;
// Note that C3 uses zero initialization by default
// so the above is equivalent to IntStack stack = {};
stack.push(1);
// The above can also be written IntStack.push(&stack, 1);
stack.push(2);
// Prints pop: 2
printf("pop: %d\n", stack.pop());
// Prints pop: 1
printf("pop: %d\n", stack.pop());
DoubleStack dstack;
dstack.push(2.3);
dstack.push(3.141);
dstack.push(1.1235);
// Prints pop: 1.1235
printf("pop: %f\n", dstack.pop());
}
- No mandatory header files
- New semantic macro system
- Module based name spacing
- Subarrays (slices)
- Compile time reflection
- Enhanced compile time execution
- Generics based on generic modules
- "Result"-based zero overhead error handling
- Defer
- Value methods
- Associated enum data
- No preprocessor
- Less undefined behaviour and runtime checks in "safe" mode
- Limited operator overloading to enable userland dynamic arrays
- Optional pre and post conditions
It's possible to try out the current C3 compiler in the browser: https://ide.judge0.com/ – this is courtesy of the developer of Judge0.
Design work on C3 is mostly done, but there are some areas that are unfinished, such as inline asm. Follow the issues here.
If you have any suggestions on how to improve the language, either file an issue or discuss C3 on its dedicated Discord: https://discord.gg/qN76R87.
The compiler should compile on Linux, Windows (under MSVC, Mingw or MSYS2) and MacOS, but needs some install documentation for Windows.
- If you wish to contribute with ideas, please file issues or discuss on Discord.
- Interested in contributing to the stdlib? Please get in touch on Discord.
- Are you a Windows dev and know your way around Github CI? Please help us get MSVC CI working!
- Install instructions for other Linux and Unix variants are appreciated.
- Would you like to contribute bindings to some library? It would be nice to have support for SDL, Raylib and more.
- Build something with C3 and show it off and give feedback. The language is still open for significant tweaks.
- Start work on the C -> C3 converter which takes C code and does a "best effort" to translate it to C3. The first version only needs to work on C headers.
- Do you have some specific area you have deep knowledge of and could help make C3 even better at doing? File or comment on issues.
- Make sure you have a C compiler that handles C11 and a C++ compiler, such as GCC or Clang. Git also needs to be installed.
- Install CMake:
sudo apt install cmake
- Install LLVM 11:
sudo apt-get install clang-11 zlib1g zlib1g-dev libllvm11 llvm-11 llvm-11-dev llvm-11-runtime liblld-11-dev liblld-11
- Clone the C3C github repository:
git clone https://github.com/c3lang/c3c.git
- Enter the C3C directory
cd c3c
. - Create a build directory
mkdir build
- Change directory to the build directory
cd build
- Set up CMake build for debug:
cmake -DLLVM_DIR=/usr/lib/llvm-11/cmake -DCMAKE_BUILD_TYPE=Debug ..
- Build:
cmake --build .
You should now have a c3c
executable.
You can try it out by running some sample code: ./c3c compile ../resources/examples/hash.c3
You can build c3c
using either an Ubuntu 18.04 or 20.04 container:
./build-with-docker.sh 18
Replace 18
with 20
to build through Ubuntu 20.04.
For a release build specify:
./build-with-docker.sh 20 Release
A c3c
executable will be found under bin/
.
- Install CMake:
brew install cmake
- Install LLVM 13:
brew install llvm
- Clone the C3C github repository:
git clone https://github.com/c3lang/c3c.git
- Enter the C3C directory
cd c3c
. - Create a build directory
mkdir build
- Change directory to the build directory
cd build
- Set up CMake build for debug:
cmake ..
- Build:
cmake --build .
Create a main.c3
file with:
module hello_world;
import std::io;
fn void main()
{
io::println("Hello, world!");
}
Make sure you have the standard libraries at either ../lib/std/
or /lib/std/
.
Then run
c3c compile main.c3
The generated binary will be called a.out
.