3.1 Communicating with C

The language indicant C is used to access C variables and calling C functions:

nest C "[&]symbol"

The row char denotation should contain a symbol corresponding to a C variable or function, optionally preceded by an ampersand &. It is the responsibility of the programmer to make sure the specified symbol actually corresponds to an entity with the right type. In case a malformed symbol is specified it will very likely result in an assembler error.

For example, this is how we can access a C variable as an Algol 68 constant:

int counter = nest C "counter";

if counter = 0
then ... fi;

If we wanted to be able to change the value of the C variable counter then we need to add a leading ampersand to the row denotation, and of course change the mode on the Algol 68 side to a ref int:

ref int counter = nest C "&counter";

counter +:= 1;

If we wanted to access a C pointer variable int *ptr as an Algol 68 name we could do:

ref int ptr = nest C "ptr";

ptr := 100; { Changes the value pointed by the C pointer ptr,
              not of ptr itself }

If we wanted to call the standard POSIX functions random and srandom we could do:

proc long int random = nest C "random";
proc(bits)void srandom = nest C "srandom";

if random < 100 then ... fi

The modes accepted in a formal hole for C are:

void

As C void.

bool

As C bool.

char

As C 32-bit integer.

int

As C int.

short int

As C short.

short short int

As C char.

long int

As C long or as C int.

long long int

As C long long or as C long or as C int.

bits

As C unsigned int.

short bits

As C unsigned short.

short short bits

As C unsigned char.

long bits

As C unsigned long or as C unsigned int.

long long bits

As C unsigned long long or as C unsigned long or as C unsigned int.

real

As C float

long real

As C double

proc with accepted formal parameter modes and yielded mode

As the corresponding C functions.

Structs with fields of accepted modes

As the corresponding C structs.