-
Notifications
You must be signed in to change notification settings - Fork 160
/
typedef.c
82 lines (65 loc) · 1.71 KB
/
typedef.c
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
/*
# typedef
Create new types based on old ones
On libc, the convention append `_t` to typedefs is used
ex: `size_t`, `wchar_t`, etc.
Some of those macros are defined to be strcitly integer types (like size_t)
while others can be either integer or floating points according to the implementation.
To print integer typedefs such with `printf`, see `printf`.
*/
#include "common.h"
int main(void) {
{
typedef int Newint;
Newint i = 1;
assert(sizeof(Newint) == sizeof(int));
}
/* ERROR: unlike macros, typedef has scope just like that of variables: */
{
/*Newint i = 1;*/
}
/*
typedef position is very flexible.
Only use the first though if you want to be sane.
*/
{
typedef unsigned int uint;
/* WARN: GCC compiles but warns you about this insanity. */
/*unsigned typedef int vint;*/
/*unsigned int typedef wint;*/
/* ERROR. This is the only one that fails to compile. */
/*unsigned int xint typedef;*/
}
/*
Repeated typedef:
http://stackoverflow.com/questions/8594954/repeated-typedefs-invalid-in-c-but-valid-in-c
Allowed in C++ and C11, forbidden in C99.
*/
{
#if __STDC_VERSION__ >= 201112L
typedef int i;
typedef int i;
i j = 0;
#endif
}
/* Multiple typedefs in one statement. */
{
typedef int i, j;
i k = 0;
j l = 1;
k = l;
}
/*
# typdef pointer
The pointer sticks to the typedef.
*/
{
typedef int * ip_t;
/* Both are pointer types. */
ip_t ip, jp;
int i, j;
ip = &i;
jp = &j;
}
return EXIT_SUCCESS;
}