I think my OCD kicked in a bit here, here is the result:
#include <stdio.h>
#include <limits.h>
#define SIZEOF_CHAR sizeof(char)
#define SIZEOF_INT sizeof(int)
#define SIZEOF_LONG sizeof(long)
#define SIZEOF_POINTER sizeof(void *)
#define NIBBLE_BIT 4
#ifndef CHAR_BIT
#define CHAR_BIT 8 // should have been defined in <limits.h>
#endif
#define INT_BIT (SIZEOF_INT * CHAR_BIT)
#define LONG_BIT (SIZEOF_LONG * CHAR_BIT)
#define POINTER_BIT (SIZEOF_POINTER * CHAR_BIT)
int main(void)
{
char hexchar[SIZEOF_CHAR * 2 + 1],
hexint[SIZEOF_INT * 2 + 1],
hexlong[SIZEOF_LONG * 2 + 1],
hexpointer[SIZEOF_POINTER * 2 + 1];
int strlen_hexchar, strlen_hexint, strlen_hexlong, strlen_hexpointer;
strlen_hexchar = sprintf(hexchar, "%x", (unsigned char)-1);
strlen_hexint = sprintf(hexint, "%x", (unsigned int)-1);
strlen_hexlong = sprintf(hexlong, "%x", (unsigned long)-1l);
strlen_hexpointer = sprintf(hexpointer, "%p", (void*)-1l);
printf("#define SIZEOF_CHAR sizeof(char) // %2d\n", SIZEOF_CHAR);
printf("#define SIZEOF_INT sizeof(int) // %2d\n", SIZEOF_INT);
printf("#define SIZEOF_LONG sizeof(long) // %2d\n", SIZEOF_LONG);
printf("#define SIZEOF_POINTER sizeof(void *) // %2d\n", SIZEOF_POINTER);
printf("\n");
printf("#define NIBBLE_BIT %-2d\n", NIBBLE_BIT);
printf("#ifndef CHAR_BIT\n");
printf("#define CHAR_BIT %-2d // should have been defined in <limits.h>\n", CHAR_BIT);
printf("#endif\n");
printf("#define INT_BIT (SIZEOF_INT * CHAR_BIT) // %2d\n", INT_BIT);
printf("#define INT_LONG (INT_LONG * CHAR_BIT) // %2d\n", LONG_BIT);
printf("#define POINTER_BIT (SIZEOF_POINTER * CHAR_BIT) // %2d\n", POINTER_BIT);
printf("\n");
printf("\nTest setup...\n");
printf("\n");
printf("char hexchar[CHAR_BIT * SIZEOF_CHAR + 1],\n");
printf(" hexint[CHAR_BIT * SIZEOF_INT + 1],\n");
printf(" hexlong[CHAR_BIT * SIZEOF_LONG + 1],\n");
printf(" hexpointer[CHAR_BIT * SIZEOF_POINTER + 1];\n");
printf("int strlen_hexchar, strlen_hexint, strlen_hexlong, strlen_hexpointer;\n");
printf("\n");
printf("strlen_hexchar = sprintf(hexchar, \"%%x\", (unsigned char)-1);\n// returned %d, hexchar populated with \"%s\"\n",
strlen_hexchar, hexchar);
printf("strlen_hexint = sprintf(hexint, \"%%x\", (unsigned int)-1);\n// returned %d, hexint populated with \"%s\"\n",
strlen_hexint, hexint);
printf("strlen_hexlong = sprintf(hexlong, \"%%x\", (unsigned long)-1);\n// returned %d, hexlong populated with \"%s\"\n",
strlen_hexlong, hexlong);
printf("strlen_hexpointer = sprintf(hexpointer, \"%%x\", (void*)-1l);\n// returned %d, hexpointer populated with \"%s\"\n",
strlen_hexpointer, hexpointer);
printf("\n\nTest results...\n");
printf("\n");
if (SIZEOF_CHAR * 2 == strlen_hexchar) {
printf("testing (SIZEOF_CHAR * 2 == strlen_hexchar) [pass]\n");
} else {
printf("testing (SIZEOF_CHAR * 2 == strlen_hexchar) [fail]\n");
printf(" (%d != $d)\n", SIZEOF_CHAR * 2, strlen_hexchar);
}
if (SIZEOF_INT * 2 == strlen_hexint) {
printf("testing (SIZEOF_INT * 2 == strlen_hexint) [pass]\n");
} else {
printf("testing (SIZEOF_INT * 2 == strlen_hexint) [fail]\n");
printf(" (%d != $d)\n", SIZEOF_INT * 2, strlen_hexint);
}
if (SIZEOF_LONG * 2 == strlen_hexlong) {
printf("testing (SIZEOF_LONG * 2 == strlen_hexlong) [pass]\n");
} else {
printf("testing (SIZEOF_LONG * 2 == strlen_hexlong) [fail]\n");
printf(" (%d != $d)\n", SIZEOF_LONG * 2, strlen_hexlong);
}
if (SIZEOF_POINTER * 2 == strlen_hexpointer) {
printf("testing (SIZEOF_POINTER * 2 == strlen_hexpointer) [pass]\n");
} else {
printf("testing (SIZEOF_POINTER * 2 == strlen_hexpointer) [fail]\n");
printf(" (%d != $d)\n", SIZEOF_POINTER * 2, strlen_hexpointer);
}
printf("\n");
if (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) {
printf("testing (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) [pass]\n");
} else {
printf("testing (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) [fail]\n");
printf(" (%d != $d)\n", CHAR_BIT, strlen_hexchar * NIBBLE_BIT);
}
if (INT_BIT == strlen_hexint * NIBBLE_BIT) {
printf("testing (INT_BIT == strlen_hexint * NIBBLE_BIT) [pass]\n");
} else {
printf("testing (INT_BIT == strlen_hexint * NIBBLE_BIT) [fail]\n");
printf(" (%d != $d)\n", INT_BIT, strlen_hexint * NIBBLE_BIT);
}
if (LONG_BIT == strlen_hexlong * NIBBLE_BIT) {
printf("testing (LONG_BIT == strlen_hexlong * NIBBLE_BIT) [pass]\n");
} else {
printf("testing (LONG_BIT == strlen_hexlong * NIBBLE_BIT) [fail]\n");
printf(" (%d != $d)\n", LONG_BIT, strlen_hexlong * NIBBLE_BIT);
}
if (POINTER_BIT == strlen_hexpointer * 4) {
printf("testing (POINTER_BIT == strlen_hexpointer * NIBBLE_BIT) [pass]\n");
} else {
printf("testing (POINTER_BIT == strlen_hexpointer * NIBBLE_BIT) [fail]\n");
printf(" (%d != $d)\n", POINTER_BIT, strlen_hexpointer * NIBBLE_BIT);
}
printf("\n");
if ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) {
printf("testing ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) [pass]\n");
} else {
printf("testing ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) [fail]\n");
printf(" (%d != %d)\n", (int)(SIZEOF_POINTER * CHAR_BIT), strlen_hexpointer * NIBBLE_BIT);
}
printf("\nConclusion: this machine word is %d bytes and %d bits\n", SIZEOF_POINTER * 8 / CHAR_BIT, strlen_hexpointer * NIBBLE_BIT);
if ((int)(SIZEOF_POINTER * CHAR_BIT) != strlen_hexpointer * NIBBLE_BIT) {
printf(" * however this conclusion did not pass the (int)(SIZEOF_POINTER * 8 / CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) test\n");
}
return 0;
}
The output from this code shows the following on my machine:
$ sizeofword.exe # from mingw32 shell on windows7
#define SIZEOF_CHAR sizeof(char) // 1
#define SIZEOF_INT sizeof(int) // 4
#define SIZEOF_LONG sizeof(long) // 4
#define SIZEOF_POINTER sizeof(void *) // 4
#define NIBBLE_BIT 4
#ifndef CHAR_BIT
#define CHAR_BIT 8 // should have been defined in <limits.h>
#endif
#define INT_BIT (SIZEOF_INT * CHAR_BIT) // 32
#define INT_LONG (INT_LONG * CHAR_BIT) // 32
#define POINTER_BIT (SIZEOF_POINTER * CHAR_BIT) // 32
Test setup...
char hexchar[CHAR_BIT * SIZEOF_CHAR + 1],
hexint[CHAR_BIT * SIZEOF_INT + 1],
hexlong[CHAR_BIT * SIZEOF_LONG + 1],
hexpointer[CHAR_BIT * SIZEOF_POINTER + 1];
int strlen_hexchar, strlen_hexint, strlen_hexlong, strlen_hexpointer;
strlen_hexchar = sprintf(hexchar, "%x", (unsigned char)-1);
// returned 2, hexchar populated with "ff"
strlen_hexint = sprintf(hexint, "%x", (unsigned int)-1);
// returned 8, hexint populated with "ffffffff"
strlen_hexlong = sprintf(hexlong, "%x", (unsigned long)-1);
// returned 8, hexlong populated with "ffffffff"
strlen_hexpointer = sprintf(hexpointer, "%x", (void*)-1l);
// returned 8, hexpointer populated with "FFFFFFFF"
Test results...
testing (SIZEOF_CHAR * 2 == strlen_hexchar) [pass]
testing (SIZEOF_INT * 2 == strlen_hexint) [pass]
testing (SIZEOF_LONG * 2 == strlen_hexlong) [pass]
testing (SIZEOF_POINTER * 2 == strlen_hexpointer) [pass]
testing (CHAR_BIT == strlen_hexchar * NIBBLE_BIT) [pass]
testing (INT_BIT == strlen_hexint * NIBBLE_BIT) [pass]
testing (LONG_BIT == strlen_hexlong * NIBBLE_BIT) [pass]
testing (POINTER_BIT == strlen_hexpointer * NIBBLE_BIT) [pass]
testing ((int)(SIZEOF_POINTER * CHAR_BIT) == strlen_hexpointer * NIBBLE_BIT) [pass]
Conclusion: this machine word is 4 bytes and 32 bits