64 Bit C Datatypes

Programming, for all ages and all languages.
Post Reply
User avatar
crazygray1
Member
Member
Posts: 168
Joined: Thu Nov 22, 2007 7:18 pm
Location: USA,Hawaii,Honolulu(Seriously)

64 Bit C Datatypes

Post 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..
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: 64 Bit C Datatypes

Post 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)
Last edited by Brynet-Inc on Sun Jan 06, 2008 10:32 pm, edited 1 time in total.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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.)
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
User avatar
crazygray1
Member
Member
Posts: 168
Joined: Thu Nov 22, 2007 7:18 pm
Location: USA,Hawaii,Honolulu(Seriously)

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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?
Post Reply