Determine word sizes by cross-compiling only


/ Published in: C
Save to your folder(s)

You can find out the word sizes and endianness of a device by compiling the following program with a cross compiler and looking at the executable with the `strings` program or a hex editor. The `TESTIF` macro puts a copy of the statement to test and its truth value into a string which will be present in the exe. This helps if you do not have such a device handy, or if it is cumbersome to access (requires loading firmware etc.).


Copy this code and paste it in your HTML
  1. #include <stdio.h>
  2. #include <endian.h>
  3.  
  4. #define TESTIF(a) printf((a)? (#a ": TRUE") : (#a ": FALSE"));
  5.  
  6. int main(int argc, char **argv)
  7. {
  8. TESTIF(sizeof(short) == 2)
  9. TESTIF(sizeof(int) == 4)
  10. TESTIF(sizeof(long) == 4)
  11. TESTIF(sizeof(long long) == 8)
  12. TESTIF(sizeof(void*) == 4)
  13. TESTIF(__BYTE_ORDER == __LITTLE_ENDIAN)
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.