Initialization

Basic types / Nullable types

// C has no optional type. A value that may be missing
// is a pointer (nullptr means no value) ...
int answer = 42;
int *number = &answer;
int *nullInt = nullptr;

// ... or a struct with a flag.
typedef struct {
    bool hasValue;
    double value;
} OptionalDouble;

OptionalDouble pi = {.hasValue = true, .value = 3.14};
OptionalDouble noValue = {};
//noValue.hasValue is false: {} sets every field to zero

printf("number is %d\n", *number);
printf("nullInt is %s\n", nullInt == nullptr ? "nullptr" : "set");
printf("pi is %.2f\n", pi.value);
printf("noValue.hasValue is %s\n", noValue.hasValue ? "true" : "false");