The example shows how to create and inspect symbols.
Output
$ ./symbol 0
the hash of 42 is 281474976710698
the hash of x is 562949963481760
the hash of x(42,x) is 1407374893613792
42 is equal to 42
42 is not equal to x
42 is less than x
Code
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
typedef struct {
char *string;
size_t string_n;
} string_buffer_t;
void free_string_buffer(string_buffer_t *buf) {
if (buf->string) {
free(buf->string);
buf->string = NULL;
buf->string_n = 0;
}
}
bool ret = true;
char *string;
size_t n;
if (buf->string_n < n) {
if (!(string = (char*)realloc(buf->string, sizeof(*buf->string) * n))) {
goto error;
}
buf->string = string;
buf->string_n = n;
}
printf("%s", buf->string);
goto out;
error:
ret = false;
out:
return ret;
}
int main() {
char const *error_message;
int ret = 0;
string_buffer_t buf = { NULL, 0 };
size_t size;
for (size_t i = 0; i < sizeof(symbols) / sizeof(*symbols); ++i) {
printf("the hash of ");
if (!print_symbol(symbols[i], &buf)) { goto error; }
}
assert(size == 2);
for (size_t i = 0; i < size; ++i) {
if (!print_symbol(symbols[0], &buf)) { goto error; }
if (!print_symbol(args[i], &buf)) { goto error; }
printf("\n");
}
if (!print_symbol(symbols[0], &buf)) { goto error; }
if (!print_symbol(symbols[1], &buf)) { goto error; }
printf("\n");
goto out;
error:
printf("%s\n", error_message);
out:
free_string_buffer(&buf);
return ret;
}