File: types.c

package info (click to toggle)
newlisp 10.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 6,248 kB
  • sloc: ansic: 33,280; lisp: 4,181; sh: 609; makefile: 215
file content (265 lines) | stat: -rw-r--r-- 7,029 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* types.c -- find out size of data types */

/* 

output on Mac OS X PPCi, memory model ILP32

type        bytes
-----------------
char          1
char *        4
void *        4
short int     2
int           4
long          4
long int      4
long long int 8
float         4
double        8
wchar_t       4
size_t        4
off_t         4
time_t        4
intptr_t      4

output on Windows MinGW 32-bit

type        bytes
-----------------
char          1
char *        4
void *        4
short int     2
int           4
long          4
long int      4
long long int 8
float         4
double        8
long double   12
wchar_t       2
size_t        4
off_t         4
time_t        4
intptr_t      4

output on AMD64 and Intel dual Core 2, memory model LP64

type        bytes
-----------------
char          1
char *        8
void *        8
short int     2
int           4 
long          8
long int      8
long long int 8
float         4
double        8
long double  16
wchar_t       4
size_t        8
off_t         8
time_t        8
intptr_t      8

LLP64 Windows when compiling with tdm gcc 64-bit

type        bytes
-----------------
char          1
char *        8
void *        8
short int     2
int           4
long          4
long int      4
long long int 8
float         4
double        8
long double   16
wchar_t       2
size_t        8
off_t         4
time_t        8
intptr_t      8

*/

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <fcntl.h>
#include <locale.h>
#include <inttypes.h>

#ifndef time_t
#include <time.h>
#endif

typedef struct {
    char one;
    short int two;
    int four;
    } alignment1;

typedef struct {
    char one;
    int four;
    short int two;
    } alignment2;

typedef struct {
    char one;
    short int two;
    long three;
    int four;
    } alignment3;

typedef struct {
    char one;
    long three;
    short int two;
    int four;
    } alignment4;

#define str2cmp(s1,s2) ( ( (*(unsigned char *)(s1) << 8) |  *((unsigned char *)(s1) + 1) ) - \
						 ( (*(unsigned char *)(s2) << 8) |  *((unsigned char *)(s2) + 1) )  )		 
void printMemoryModel(void);

int main(int argc, char * argv[])
{
int endian = 1;
long long int x, y, z;

alignment1 checkstruct1;
alignment2 checkstruct2;
alignment3 checkstruct3;
alignment4 checkstruct4;

printf("\n");
printf("type        bytes\n");
printf("-----------------\n");
printf("char          %lu\n", sizeof(char));
printf("char *        %lu\n", sizeof(char *));
printf("void *        %lu\n", sizeof(void *));
printf("short int     %lu\n", sizeof(short int));
printf("int           %lu\n", sizeof(int));
printf("long          %lu\n", sizeof(long));
printf("long int      %lu\n", sizeof(long int));
printf("long long int %lu\n", sizeof(long long int));
printf("float         %lu\n", sizeof(float));
printf("double        %lu\n", sizeof(double));
printf("long double   %lu\n", sizeof(long double));
printf("wchar_t       %lu\n", sizeof(wchar_t));
printf("size_t        %lu\n", sizeof(size_t));
printf("off_t         %lu\n", sizeof(off_t));
printf("time_t        %lu\n", sizeof(time_t));
#if defined(_INTTYPES_H_)
printf("intptr_t      %lu\n", sizeof(intptr_t));
#endif

printf("\nAlignment:\n");
printf("size of struct {char, short int, int} is: %ld\n", sizeof(checkstruct1));
printf("size of struct {char, int, short int} is: %ld\n", sizeof(checkstruct2));
printf("size of struct {char, short int, long, int} is: %ld\n", sizeof(checkstruct3));
printf("size of struct {char, long, short int, int} is: %ld\n", sizeof(checkstruct4));
printf("\n");

printf("format input                  output\n");
printf("--------------------------------------------------\n");
printf("%%d     0x7fffffff             %d\n", 0x7fffffff);
printf("%%d     0x80000000             %d\n", 0x80000000);
printf("%%d     0xffffffff             %d\n", 0xffffffff);
printf("%%u     0x7fffffff             %u\n", 0x7fffffff);
printf("%%u     0x80000000             %u\n", 0x80000000);
printf("%%u     0xffffffff             %u\n", 0xffffffff);
#ifndef TRUE64
/* will handle either 32bit or 64bit depending on platform
printf("%%ld   0x7fffffffffffffffL    %ld\n", 0x7FFFFFFFL);
printf("%%ld   0x8000000000000000L    %ld\n", 0x80000000L);
printf("%%ld   0xffffffffffffffffL    %ld\n", 0xFFFFFFFFL);
printf("%%lu   0x7fffffffffffffffL    %lu\n", 0x7FFFFFFFL);
printf("%%lu   0x8000000000000000L    %lu\n", 0x80000000L);
printf("%%lu   0xffffffffffffffffL    %ld\n", 0xFFFFFFFFL);
*/
#endif
printf("%%llx   0x7FFFFFFFFFFFFFFFLL   %llx\n", 0x7FFFFFFFFFFFFFFFLL);
printf("%%llx   0x8000000000000000LL   %llx\n", 0x8000000000000000LL);
printf("%%llx   0xFFFFFFFFFFFFFFFFLL   %llx\n", 0xFFFFFFFFFFFFFFFFLL);
printf("%%llX   0x7fffffffffffffffLL   %llX\n", 0x7fffffffffffffffLL);
printf("%%llX   0x8000000000000000LL   %llX\n", 0x8000000000000000LL);
printf("%%llX   0xffffffffffffffffLL   %llX\n", 0xffffffffffffffffLL);
#ifndef TRU64
printf("%%lld   0x7fffffffffffffffLL   %lld\n", 0x7fffffffffffffffLL);
printf("%%lld   0x8000000000000000LL   %lld\n", 0x8000000000000000LL);
printf("%%lld   0xffffffffffffffffLL   %lld\n", 0xffffffffffffffffLL);
printf("%%llu   0x7fffffffffffffffLL   %lld\n", 0x7fffffffffffffffLL);
printf("%%llu   0x8000000000000000LL   %llu\n", 0x8000000000000000LL);
printf("%%llu   0xffffffffffffffffLL   %llu\n", 0xffffffffffffffffLL);
#else
printf("%%ld   0x7fffffffffffffffLL    %ld\n", 0x7FFFFFFFFFFFFFFFLL);
printf("%%ld   0x8000000000000000LL    %ld\n", 0x8000000000000000LL);
printf("%%ld   0xffffffffffffffffLL    %ld\n", 0xFFFFFFFFFFFFFFFFLL);
printf("%%lu   0x7fffffffffffffffLL    %lu\n", 0x7FFFFFFFFFFFFFFFLL);
printf("%%lu   0x8000000000000000LL    %lu\n", 0x8000000000000000LL);
printf("%%lu   0xffffffffffffffffLL    %ld\n", 0xFFFFFFFFFFFFFFFFLL);
#endif

printf("\n");

if(*(char *)&endian == 1)
	printf("CPU is little endian\n");
else
	printf("CPU is big endian\n");
	
#ifdef __LITTLE_ENDIAN__
printf("The constant __LITTLE_ENDIAN__ is defined by the compiler\n");
#endif

#ifdef __BIG_ENDIAN__
printf("The constant __BIG_ENDIAN__ is defined by the compiler\n");
#endif

printMemoryModel();
printf("\n");

/*
x = 9223372036854775807LL; 
y = 1000LL;
z = x * y;
printf("\n9223372036854775807 * 1000 = %ld <- should be -1000\n", z);

x = 11;
y = -4;
z = x % y;

printf("11 %% -4 = %ld <- should be 3\n", z);

setlocale(LC_NUMERIC, "");
printf("$%'.2Lf\n", 123456789.00L);
printf("$%'.2Lf\n", 1234.56L);
printf("$%'.2Lf\n", 123.45L);
*/


exit(0);
}

void printMemoryModel(void)
{
short sc = sizeof(char) * 8;
short ss = sizeof(short) * 8;
short si = sizeof(int) * 8;
short sl = sizeof(long) * 8;
short sp = sizeof(void*) * 8;

printf("\nMemory model is ");
if (si == 32 && sl == 64 && sp == 64) { printf("LP64\n"); return;}
if (si == 64 && sl == 64 && sp == 64) { printf("ILP64\n"); return;}
if (si == 32 && sl == 32 && sp == 64) { printf("LLP64\n"); return;}
if (si == 32 && sl == 32 && sp == 32) { printf("ILP32\n"); return;}
if (si == 16 && sl == 32 && sp == 32) { printf("LP32\n"); return;}
printf("UNKNOWN\n"); 
}