1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

union NumberValue {
    int as_int;
    double as_double;
    char as_char;
};

enum NumberType { INT, DOUBLE, CHAR };

struct Number {  // TWO fields
    union NumberValue value;
    enum NumberType which;
};

void print_number(struct Number n) {
    if(n.which == INT) {
        printf("%d\n", n.value.as_int);
    }
    else if(n.which == DOUBLE) {
        printf("%f\n", n.value.as_double);
    }
    else if(n.which == CHAR) {
        printf("'%c'\n", n.value.as_char);
    }
}
int main(int argc, char* argv[]) {
    struct Number n = { .value.as_double = 3.1415962, .which = DOUBLE };
    print_number(n);
    return EXIT_SUCCESS;
}

/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2024 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.