Subject: Types sizes in C
To: None <port-sparc64@netbsd.org>
From: khorben <khorben@defora.org>
List: port-sparc64
Date: 04/07/2006 23:25:43
Hello port-sparc64,
I have been recently surprised to see that the following C program has a
different output on Solaris 10 and NetBSD 3, for the same hardware (and
both while using gcc):
#include <stdio.h>
#define type(a) printf("sizeof(%s) = %d\n", "" # a, sizeof(a));
int main(void)
{
type(char);
type(short);
type(int);
type(float);
type(double);
type(long);
type(long long);
return 0;
}
So on Solaris 10 I obtain:
$ ./sizes
sizeof(char) = 1
sizeof(short) = 2
sizeof(int) = 4
sizeof(float) = 4
sizeof(double) = 8
sizeof(long) = 4
sizeof(long long) = 8
while NetBSD 3 gives:
$ ./sizes
sizeof(char) = 1
sizeof(short) = 2
sizeof(int) = 4
sizeof(float) = 4
sizeof(double) = 8
sizeof(long) = 8
sizeof(long long) = 8
ok, the only difference is actually the size of long integers,
respectively 32 and 64 bits. From my reading of the ANSI C standard I
understood this is possible.
I have a few questions about this though, if appropriate:
- who sets this?
- why not keep long as 32 bits and let int default to 64 bits instead?
This would help the short/long/long long hierarchy coherence, and let
int default to the native processor size (and 486 would be 16 bits!)
- has this ever been observed to cause portability or stability issues
on this platform?
My concern is, I have seen programmers assume sizeof(int) or
sizeof(long) are 32 bits, even while writing portable 32/64 bits code
(and I was myself wrong at first, since it is apparently very platform
dependant even on 64 bits hardware).
Morale => #include <stdint.h> ?
HTH,
--
khorben