Skip to content

Builtin Functions

Spice offers five builtin functions out of the box. Those can be used anywhere without having to be imported manually and can be used to establish a minimal setup for testing or the like.

The printf builtin

Printf works the same as the printf function in C and is designed for printing a string to the standard text output (cout).

Signature

void printf(string template, ...args)

template: Template string, which can contain placeholders for values, passed as args to the printf builtin.
args: Arbitrary number of arguments of any type. The particular type and the order of the types have to match the placeholders of the template string.

Within the template string you can use control characters like the \n control character to achieve line breaks. This works on Linux and Windows the same way.

Placeholders

Placeholder Type Output Example
%c char Character a
%d/%i int, short, long Signed decimal integer 392
%u int, short, long Unsigned decimal integer 7235
%o int, short, long Unsigned octal 610
%x int, short, long Unsigned hexadecimal integer 7fa
%X int, short, long Unsigned hexadecimal integer (uppercase) 7FA
%lld int, short, long Signed decimal long integer 7235
%llu int, short, long Unsigned decimal long integer 392
%a double Hexadecimal floating point (lowercase) -0xc.90fep-2
%A double Hexadecimal floating point (uppercase) -0XC.90FEP-2
%f double Decimal floating point (lowercase) 392.65
%F double Decimal floating point (uppercase) 392.65
%e double Scientific notation (lowercase) 3.9265e+2
%E double Scientific notation (uppercase) 3.9265E+2
%g double Use the shortest representation: %e or %f 392.65
%G double Use the shortest representation: %e or %f 392.65
%s string, char*, char[] String of characters sample
%p <any>* Pointer address b8000000
%% - Escape character for printing a single % %

Usage example

printf("Here is a string: %s.\nAnd here is a double: %f", "Demo", 1.123);

The sizeof builtin

Sizeof returns the internal size of a variable or a constant in bits. To get the size in bytes, simply divide the result by 8.

Signature

int sizeof(<any variable>) int sizeof(type <any type>)

any variable: Variable or constant of any type. any type: Any data type

If the variable is a pointer type, the size of the contained type is being returned.

Usage example

sizeof(12); // 32
sizeof(type int) // 32

int[9] intArray = {};
sizeof(intArray); // 9 * 32 = 288

sizeof("Hello World!"); // 64 (Strings are Char pointers internally)

The len builtin

Len returns the length of a Spice array in items.

Signature

int len(any[] variable)

variable: Variable of any array type.

Usage example

len({1, 2, 3, 4}); // 4

string[5] stringArray = {"string1", "string2", "string3"};
len(stringArray); // 5

The tid builtin

Tid returns the thread id of the current thread.

Signature

int tid()

Usage example

int threadId = tid();

The join builtin

Join waits for the termination of the one or more given threads. It returns the number of joined threads as an int.

Signature

int join(byte* ...threadIds)

Usage example

byte* t1 = thread {
    usleep(300 * 1000);
    printf("Thread 1 finished\n");
};

byte* t2 = thread {
    usleep(100 * 1000);
    printf("Thread 2 finished\n");
};

int threadsJoined = join(t1, t2);