top | item 19240325

(no title)

dianeb | 7 years ago

I wrote C compilers for several large companies in the 80's and 90's. Everyone in a compiler group had something like this if for no other reason than to demonstrate why "bugs" weren't bugs but "features". My code -- very different than this -- works to this day, but only because I keep it up-to-date with the C standard. Over the years, it's been very useful, especially when moving from one platform to another.

Most code really doesn't need this much detail -- "sizeof" is your friend -- You can start with something like this:

  sizes.c

  #include <stdio.h>

  int isBigEndian() {
    int x = 1;
    return(*(char *)&x != 1);
  }

  int main() {
    printf("This is a %s endian machine\n", isBigEndian() ? "big" : "little");
    printf("char: %lu\n", sizeof(char));
    printf("short: %lu\n", sizeof(short));
    printf("int: %lu\n", sizeof(int));
    printf("long: %lu\n", sizeof(long));
    printf("long long: %lu\n", sizeof(long long));
    printf("float: %lu\n", sizeof(float));
    printf("double: %lu\n", sizeof(double));
    printf("long double: %lu\n", sizeof(long double));
  }
compile as: cc -std=c17 sizes.c

...and add whatever else you need as you go along. A version of my compiler information code is in my forthcoming book.

discuss

order

kqr2|7 years ago

Is there a link to your upcoming book?