-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeof.c
78 lines (73 loc) · 1.5 KB
/
sizeof.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
/*
* Copyright by xichen(Xi.chen) [email protected]
*/
#include "common_local.h"
const char *type_name[] = {
"char",
"short",
"int",
"long",
"longlong",
"float",
"double",
"p", // means pointer
"off_t",
"size_t",
"ssize_t",
};
const int type_sizeof[] = {
sizeof(char),
sizeof(short),
sizeof(int),
sizeof(long),
sizeof(long long),
sizeof(float),
sizeof(double),
sizeof(void *),
sizeof(off_t),
sizeof(size_t),
sizeof(ssize_t),
};
// if return -1, it means the type is unknown!
int get_size_from_type_arr(const char *type)
{
int i;
for(i = 0; i < sizeof(type_name) / sizeof(type_name[0]); ++i) {
if(strcmp(type, type_name[i]) == 0)
return type_sizeof[i];
}
return -1;
}
int main(int argc, char *argv[])
{
int a, b, c, d;
LOG_W("Owner: Xi Chen!\n");
if(argc == 1) { // print all sizeof infos
a = sizeof(char);
b = sizeof(short);
c = sizeof(int);
d = sizeof(long);
// it's ugly!
printf("sizeof(char):%d\n", a);
printf("sizeof(short):%d\n", b);
printf("sizeof(ing):%d\n", c);
printf("sizeof(long):%d\n", d);
printf("sizeof(long long):%lu\n", sizeof(long long));
printf("sizeof(float):%lu\n", sizeof(float));
printf("sizeof(double):%lu\n", sizeof(double));
printf("sizeof(void *):%lu\n", sizeof(void *));
return 0;
}
if(argc > 1) {
int i = 1;
while(i < argc) {
int size = get_size_from_type_arr(argv[i]);
if(size >= 0) {
printf("sizeof(%s):%d\n", argv[i], size);
} else
printf("Unknown type:%s\n", argv[i]);
++i;
}
}
return 0;
}