File: sizeof.c

package info (click to toggle)
c2go 0.26.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: ansic: 6,037; sh: 82; makefile: 5
file content (137 lines) | stat: -rw-r--r-- 2,431 bytes parent folder | download | duplicates (3)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This file contains tests for the sizeof() function and operator.

#include <stdio.h>
#include "tests.h"

#define check_sizes(type, size)         \
    is_eq(sizeof(type), size);          \
    is_eq(sizeof(unsigned type), size); \
    is_eq(sizeof(signed type), size);   \
    is_eq(sizeof(const type), size);    \
    is_eq(sizeof(volatile type), size);

#define FLOAT(type, size) \
    is_eq(sizeof(type), size);

#define OTHER(type, size) \
    is_eq(sizeof(type), size);

// We print the variable so that the compiler doesn't complain that the variable
// is unused.
#define VARIABLE(v, p) \
    printf("%s = (%d) %d bytes\n", #v, p, sizeof(v));

struct MyStruct
{
    double a;
    char b;
    char c;
};

struct MyStruct2
{
    double a;
    char b;
    char c;
    char d[10];
};

struct MyStruct3
{
    double a;
    char b;
    char c;
    char d[20];
};

struct MyStruct4
{
    double a;
    char b;
    char c;
    char d[30];
};

union MyUnion
{
    double a;
    char b;
    int c;
};

short a;
int b;

int main()
{
    plan(42);

    diag("Integer types");
    check_sizes(char, 1);
    check_sizes(short, 2);
    check_sizes(int, 4);
    check_sizes(long, 8);

    diag("Floating-point types");
    is_eq(sizeof(float), 4);
    is_eq(sizeof(double), 8);
    is_eq(sizeof(long double), 16);

    diag("Other types");
    is_eq(sizeof(void), 1);

    diag("Pointers");
    is_eq(sizeof(char *), 8);
    is_eq(sizeof(char *), 8);
    is_eq(sizeof(short **), 8);

    diag("Variables");
    a = 123;
    b = 456;
    struct MyStruct s1;
    s1.b = 0;
    struct MyStruct2 s2;
    s2.b = 0;
    struct MyStruct3 s3;
    s3.b = 0;
    struct MyStruct4 s4;
    s4.b = 0;
    union MyUnion u1;
    u1.b = 0;

    is_eq(sizeof(a), 2);
    is_eq(sizeof(b), 4);
    is_eq(sizeof(s1), 16);
    is_eq(sizeof(s2), 24);
    is_eq(sizeof(s3), 32);
    is_eq(sizeof(s4), 40);
    is_eq(sizeof(u1), 8);

    diag("Structures");
    is_eq(sizeof(struct MyStruct), 16);

    diag("Unions");
    is_eq(sizeof(union MyUnion), 8);

    diag("Function pointers");
    is_eq(sizeof(main), 1);

    diag("Arrays");
    char c[3] = {'a', 'b', 'c'};
    c[0] = 'a';
    is_eq(sizeof(c), 3);

    int *d[3];
    d[0] = &b;
    is_eq(sizeof(d), 24);

    int **e[4];
    e[0] = d;
    is_eq(sizeof(e), 32);

    const char * const f[] = {"a", "b", "c", "d", "e", "f"};
    is_eq(sizeof(f), 48);
    is_streq(f[1], "b");

    done_testing();
}