aboutsummaryrefslogtreecommitdiffstats
path: root/test/regression/bitfields9.c
blob: 025216fae4c12deebf32fcc3009090ea05cd5f33 (plain)
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
#include <stdio.h>

/* Static initialization of bit-fields */

struct s {
  signed char a: 6;
  unsigned int b: 2;
};

struct t {
  unsigned int c: 16;
  _Bool d: 1;
  short e: 8;
  int : 10;
};

union u {
  int u: 4;
  unsigned int v: 3;
};

void print_s(char * msg, struct s p)
{
  printf("%s = { a = %d, b = %d }\n", msg, p.a, p.b);
}

void print_t(char * msg, struct t p)
{
  printf("%s = { c = %d, d = %d, e = %d }\n", msg, p.c, p.d, p.e);
}

void print_u_u(char * msg, union u p)
{
  printf("%s = { u = %d }\n", msg, p.u);
}

void print_u_v(char * msg, union u p)
{
  printf("%s = { v = %u }\n", msg, p.v);
}

/* Global initialization */
struct s glob_s = { -12, 1 };
struct t glob_t = { 123, 2, -45 };
union u glob_u_u = { -3 };
union u glob_u_v = { .v = 6 };

/* Local, static initialization */
void f(void)
{
  static struct s loc_s = { -12, 1 };
  static struct t loc_t = { 123, 2, -45 };
  static union u loc_u_u = { -3 };
  static union u loc_u_v = { .v = 6 };
  print_s("loc_s", loc_s);
  print_t("loc_t", loc_t);
  print_u_u("loc_u_u", loc_u_u);
  print_u_v("loc_u_v", loc_u_v);
}

int main()
{
  print_s("glob_s", glob_s);
  print_t("glob_t", glob_t);
  print_u_u("glob_u_u", glob_u_u);
  print_u_v("glob_u_v", glob_u_v);
  f();
  return 0;
}