add code/C/packtest.c
This commit is contained in:
parent
0818f87582
commit
66bc3d19c4
1
code/C/.gitignore
vendored
1
code/C/.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
fgets-simple
|
||||
demo-strtok
|
||||
packtest
|
||||
|
@ -2,7 +2,7 @@
|
||||
# exemples pour le chapitre sur le C
|
||||
# new Sat Feb 11 12:06:34 CET 2023
|
||||
|
||||
all: no-op slowprint fgets-simple
|
||||
all: no-op slowprint fgets-simple packtest
|
||||
|
||||
no-op: no-op.c Makefile
|
||||
gcc -Wall $< -o $@
|
||||
@ -15,3 +15,7 @@ fgets-simple: fgets-simple.c Makefile
|
||||
|
||||
demo-strtok: demo-strtok.c Makefile
|
||||
gcc -Wall $< -o $@
|
||||
|
||||
# added Wed Mar 27 10:01:41 UTC 2024
|
||||
packtest: packtest.c
|
||||
gcc -Wall $< -o $@
|
||||
|
116
code/C/packtest.c
Executable file
116
code/C/packtest.c
Executable file
@ -0,0 +1,116 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/*
|
||||
* found in the good old Usenet
|
||||
*/
|
||||
|
||||
/* The expected sizes in these comments assime a 64-bit machine */
|
||||
|
||||
struct foo1 {
|
||||
char *p;
|
||||
char c;
|
||||
long x;
|
||||
};
|
||||
|
||||
struct foo2 {
|
||||
char c; /* 1 byte */
|
||||
char pad[7]; /* 7 bytes */
|
||||
char *p; /* 8 bytes */
|
||||
long x; /* 8 bytes */
|
||||
};
|
||||
|
||||
struct foo3 {
|
||||
char *p; /* 8 bytes */
|
||||
char c; /* 1 byte */
|
||||
};
|
||||
|
||||
struct foo4 {
|
||||
short s; /* 2 bytes */
|
||||
char c; /* 1 byte */
|
||||
};
|
||||
|
||||
struct foo5 {
|
||||
char c;
|
||||
struct foo5_inner {
|
||||
char *p;
|
||||
short x;
|
||||
} inner;
|
||||
};
|
||||
|
||||
struct foo6 {
|
||||
short s;
|
||||
char c;
|
||||
int flip:1;
|
||||
int nybble:4;
|
||||
int septet:7;
|
||||
};
|
||||
|
||||
struct foo7 {
|
||||
int bigfield:31;
|
||||
int littlefield:1;
|
||||
};
|
||||
|
||||
struct foo8 {
|
||||
int bigfield1:31;
|
||||
int littlefield1:1;
|
||||
int bigfield2:31;
|
||||
int littlefield2:1;
|
||||
};
|
||||
|
||||
struct foo9 {
|
||||
int bigfield1:31;
|
||||
int bigfield2:31;
|
||||
int littlefield1:1;
|
||||
int littlefield2:1;
|
||||
};
|
||||
|
||||
struct foo10 {
|
||||
char c;
|
||||
struct foo10 *p;
|
||||
short x;
|
||||
};
|
||||
|
||||
struct foo11 {
|
||||
struct foo11 *p;
|
||||
short x;
|
||||
char c;
|
||||
};
|
||||
|
||||
struct foo12 {
|
||||
struct foo12_inner {
|
||||
char *p;
|
||||
short x;
|
||||
} inner;
|
||||
char c;
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
printf("sizeof(char *) = %zu\n", sizeof(char *));
|
||||
printf("sizeof(long) = %zu\n", sizeof(long));
|
||||
printf("sizeof(int) = %zu\n", sizeof(int));
|
||||
printf("sizeof(short) = %zu\n", sizeof(short));
|
||||
printf("sizeof(char) = %zu\n", sizeof(char));
|
||||
printf("sizeof(float) = %zu\n", sizeof(float));
|
||||
printf("sizeof(double) = %zu\n", sizeof(double));
|
||||
printf("sizeof(struct foo1) = %zu\n", sizeof(struct foo1));
|
||||
printf("sizeof(struct foo2) = %zu\n", sizeof(struct foo2));
|
||||
printf("sizeof(struct foo3) = %zu\n", sizeof(struct foo3));
|
||||
printf("sizeof(struct foo4) = %zu\n", sizeof(struct foo4));
|
||||
printf("sizeof(struct foo5) = %zu\n", sizeof(struct foo5));
|
||||
printf("sizeof(struct foo6) = %zu\n", sizeof(struct foo6));
|
||||
printf("sizeof(struct foo7) = %zu\n", sizeof(struct foo7));
|
||||
printf("sizeof(struct foo8) = %zu\n", sizeof(struct foo8));
|
||||
printf("sizeof(struct foo9) = %zu\n", sizeof(struct foo9));
|
||||
printf("sizeof(struct foo10) = %zu\n", sizeof(struct foo10));
|
||||
printf("sizeof(struct foo11) = %zu\n", sizeof(struct foo11));
|
||||
printf("sizeof(struct foo12) = %zu\n", sizeof(struct foo12));
|
||||
|
||||
if (sizeof(struct foo3) == 16) {
|
||||
puts("This looks like a 64-bit machine.");
|
||||
} else if (sizeof(struct foo3) == 6) {
|
||||
puts("This looks like a 32-bit machine.");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user