For in Cilk
In Cilk, which is an extension of C, we use similar looping constructs to those found in C. Here are the main differences and adaptations:
We use
#include <stdio.h>
for input/output functions and#include <cilk/cilk.h>
for Cilk-specific features (although we don’t use any Cilk-specific features in this example).The
fmt.Println()
function is replaced withprintf()
.Cilk (and C) don’t have a direct equivalent to Go’s
range
over integers, so we use a regularfor
loop instead.The infinite loop is implemented using
while (1)
instead offor
without conditions.Variable declarations are typically done at the beginning of a block in C/Cilk, so we’ve adjusted the placement of some variable declarations.
To compile and run this Cilk program:
Note that Cilk is primarily used for parallel programming, but this example doesn’t showcase any parallelism. For more complex examples involving parallel constructs, you would use Cilk-specific keywords like cilk_spawn
and cilk_sync
.
Cilk provides powerful constructs for parallel programming, which we’ll see in later, more advanced examples.