librelist archives

« back to archive

ex7: 32 bit vs 64

ex7: 32 bit vs 64

From:
Igor Galić
Date:
2011-09-05 @ 10:38
Hey folks,

re ex7:

In C on Unix ``int'' is guaranteed to be 32 bit. Regardless of whether the
system is 32 bit or 64. (Not sure if ISO+POSIX would require this even if
the underlying architecture is, say 40 bit wide, but lets neglect that for now ;)

I've done lots of porting on an bi-arch Unix (Solaris) and this can be
a pain in the butt when you have to share the same headers for both the
32bit and the 64bit compile. int isn't the trouble maker here, it's really
just long:

  # include <stdio.h>

  int main(int argc, char *argv[])
  {
    /* n.b.: sizeof changes its return value, so we always cast to stay 
portable: */
    printf ("%u\n", (unsigned)sizeof(long));
    return 0;
  }


Compile + execution:

  igalic@tynix ~/src/lctwh % make -f long.mk
  cc -m32 -o long32 -Wall -Werror -pedantic -std=c99 long.c
  cc -D__WORDSIZE=64 -m32 -o long32Dwsize -Wall -Werror -pedantic -std=c99 long.c
  cc -m64 -o long64 -Wall -Werror -pedantic -std=c99 long.c
  cc -m64 -D__WORDSIZE=32 -o long64Dwsize -Wall -Werror -pedantic -std=c99 long.c
  igalic@tynix ~/src/lctwh % make -f long.mk exec
  echo -n "cc -m32 "
  cc -m32 ./long32
  4
  echo -n "cc -m32 -D__WORDSIZE=64 "
  cc -m32 -D__WORDSIZE=64 ./long32Dwsize
  4
  echo -n "cc -m64 "
  cc -m64 ./long64
  8
  echo -n "cc -m64 -D__WORDSIZE=32 "
  cc -m64 -D__WORDSIZE=32 ./long64Dwsize
  8


Note: My system is a 64bit system, I installed the appropriate dependencies to
build 64bit binaries and libs (i.e.: libc6-dev-i386 on Ubuntu)

Ever since tasting this side of Unix, I've learned to appreciate
inttypes.h and stdint.h

So long,
i

-- 
Igor Galić

IRC: jMCg
Twitter: @hirojin