Title here
Summary here
#include <stdio.h>
#include <cilk/cilk.h>
typedef struct {
int x, y;
} point;
int main() {
// Cilk offers several printing options for formatting values.
// For example, this prints an instance of our `point` struct.
point p = {1, 2};
printf("struct1: {%d %d}\n", p.x, p.y);
// To include the struct's field names, we need to manually format the output.
printf("struct2: {x:%d y:%d}\n", p.x, p.y);
// To print a representation similar to source code, we also need to manually format.
printf("struct3: (point){x:%d, y:%d}\n", p.x, p.y);
// To print the type of a value, we use the %s format specifier with the type name.
printf("type: %s\n", "point");
// Formatting booleans
printf("bool: %s\n", 1 ? "true" : "false");
// There are many options for formatting integers.
// Use %d for standard, base-10 formatting.
printf("int: %d\n", 123);
// This prints a binary representation.
printf("bin: %b\n", 14);
// This prints the character corresponding to the given integer.
printf("char: %c\n", 33);
// %x provides hex encoding.
printf("hex: %x\n", 456);
// There are also several formatting options for floats.
// For basic decimal formatting use %f.
printf("float1: %f\n", 78.9);
// %e and %E format the float in (slightly different versions of) scientific notation.
printf("float2: %e\n", 123400000.0);
printf("float3: %E\n", 123400000.0);
// For basic string printing use %s.
printf("str1: %s\n", "\"string\"");
// To double-quote strings, we need to manually add the quotes.
printf("str2: \"%s\"\n", "\"string\"");
// To print a hexadecimal representation of a string, we need to iterate over its characters.
const char* str = "hex this";
printf("str3: ");
for (int i = 0; str[i] != '\0'; i++) {
printf("%02x", (unsigned char)str[i]);
}
printf("\n");
// To print a representation of a pointer, use %p.
printf("pointer: %p\n", (void*)&p);
// When formatting numbers you will often want to control the width and precision of the resulting figure.
// To specify the width of an integer, use a number after the % in the format specifier.
printf("width1: |%6d|%6d|\n", 12, 345);
// You can also specify the width of printed floats, along with decimal precision.
printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45);
// To left-justify, use the - flag.
printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45);
// You may also want to control width when formatting strings, especially to ensure that they align in table-like output.
printf("width4: |%6s|%6s|\n", "foo", "b");
// To left-justify use the - flag as with numbers.
printf("width5: |%-6s|%-6s|\n", "foo", "b");
// Cilk doesn't have a direct equivalent to Go's Sprintf, but we can use snprintf to format a string.
char buffer[50];
snprintf(buffer, sizeof(buffer), "sprintf: a %s", "string");
printf("%s\n", buffer);
// To print to stderr, use fprintf.
fprintf(stderr, "io: an %s\n", "error");
return 0;
}
This Cilk code demonstrates various string formatting techniques similar to those shown in the Go example. However, there are some differences due to the nature of C and Cilk:
printf
for most formatting tasks.%b
) is not standard in C’s printf
. You might need to implement this yourself or use a different approach if binary output is needed.To compile and run this Cilk program, you would typically use:
$ cilk++ -O3 string_formatting.cilk -o string_formatting
$ ./string_formatting
This will produce output similar to the Go program, demonstrating various string formatting techniques in Cilk.