Data types and portability

Started by
6 comments, last by TerranFury 22 years, 3 months ago
I have a two questions concerning the portability of code: 1 - Is a char defined as 8 bits or as a byte? This of course affects systems with byte sizes other than 8 bits. 2 - Does sizeof(...) return a number of bytes or a number of chars?
Advertisement
1) Don''t quote me on this, but I believe that it would depend on your compiler and its settings.

2) It returns the number of bytes that the variable (or data type, whatever the case) in bytes.
To all of those who think I'm just being contradictory:No I'm not!My First Game!!!"
As was said, a char can be any size the compiler wants it to be (In limit.h there''s a macro called CHAR_BIT though). The sizeof operator gives results in the number of characters. Therefore "sizeof(char)" is always 1.

[Resist Windows XP''s Invasive Production Activation Technology!]
I understand that the maximum values of all data types are found in limits.h, and that using limits.h should be portable. However, given what you''ve just said, the following should also therefore be portable disregarding very strange hardware that represents integers in something other than two''s compliment form, regardless of byte size or endian-ness:

(1 << (sizeof(long) * CHAR_BIT)) - 1

It should give the maximum value that can be stored by a long. I am right that this is portable?
As long as the binary level math works the way you''re expecting it should be portable. I assume you know you can find that value (the highest value that a long can hold) from limits.h as well.
According to Stroustrup, it is guaranteed that a char has at least 8 bits.

He also states that "Sizes of C++ objects are expressed in terms of multiples of the size of a char, so by definition the size of a char is 1."

It seems silly to express sizes in multiples of char, since sizeof(wchar_t) will most likely return 2, even though it represents a single character.
Thanks, that clears some things up.
quote:Original post by TerranFury
I have a two questions concerning the portability of code:

1 - Is a char defined as 8 bits or as a byte? This of course affects systems with byte sizes other than 8 bits.

No

quote:2 - Does sizeof(...) return a number of bytes or a number of chars?

Bytes, though their size isn''t fixed.

I *think* that a char is always the smallest type, and hence a char is always a byte, but I''m not certain. C/C++ really doesn''t have any absolute rules about type sizes; it merely defines relationships; for instance, a long double must be at least a double, but optionally it may store more precision/larger numbers -- but nothing more than that. A long double could simply be the same as a double (and there''s at least one common platform that does just this; Win32 long doubles are standard 64-bit doubles, rather than Win16 long doubles, which used x87''s built-in 80-bit doubles.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement