Page 1 of 1

64 Bit C Datatypes

Posted: Sun Jan 06, 2008 8:59 pm
by crazygray1
Just making sure.
When you write 64 bit software:
short is 16 bits
int is 32 bits
long is 64 bits right?
add on please..

Re: 64 Bit C Datatypes

Posted: Sun Jan 06, 2008 10:24 pm
by Brynet-Inc
crazygray1 wrote:Just making sure.
When you write 64 bit software:
short is 16 bits
int is 32 bits
long is 64 bits right?
add on please..
Wrong.

On 32bit systems:
char is 8 bits..
short is 16bits..
int is typically 32bits.. (Never assume int is 32bits.. it's not always.)
long is 32bits..
"long long" would be a 64bit type..

If you're on a 64bit system, you should be aware of the "data model" being used by the operating system.

http://en.wikipedia.org/wiki/64bit#64-bit_data_models

I typically advice C programmers to use C99 types whenever possible.. (Normally found in stdint.h)
int8_t/uint8_t
int16_t/uint16_t
int32_t/uint32_t
int64_t/uint64_t

Hopefully, The OS you're using will have these types setup properly.. ;)

Have fun... 8)

Posted: Sun Jan 06, 2008 10:31 pm
by Alboin
On my 64bit setup:

Code: Select all

printf("%li %li %li %li\n", sizeof(char), sizeof(short), sizeof(int), sizeof(long));
yields:

Code: Select all

1 2 4 8
So, on 64bit machines you would be correct. (Albeit unportable.)

Posted: Sun Jan 06, 2008 10:41 pm
by Brynet-Inc
Alboin wrote:So, on 64bit machines you would be correct. (Albeit unportable.)
Right, on many 64bit Unix systems that's correct.. although... ;)

64bit Windows would have returned:
1 2 4 4

Only pointers and long long are 64bit.. LLP64 data model.

I don't know any ILP64 or SILP64 implementors.. but the latter uses 64bits for short! hah! :roll:

Posted: Mon Jan 07, 2008 12:28 am
by Solar
With regret and some irritation I have to inform you that C did not, does not, and never will make any definition of which datatype will be which width on platform xyz.

sizeof( char ) = 1
sizeof( short ) >= sizeof( char )
sizeof( int ) >= sizeof( short )
sizeof( long ) >= sizeof( int )
sizeof( long long ) >= sizeof( long )

For everything else, use the typedef's in <stdint.h>. Assumptions like "on my machine..." will only shoot you in the foot.

Posted: Mon Jan 07, 2008 8:03 am
by crazygray1
so you're saying in order to make my kernel more portable I should use a header containing all of the data-type typedefs.

Posted: Mon Jan 07, 2008 11:44 am
by Candy
crazygray1 wrote:so you're saying in order to make my kernel more portable I should use a header containing all of the data-type typedefs.
What about the freestanding, fully portable and compiler-delivered stdint.h? Or it's C++ cousin, cstdint?