OT: Requesting C advice

Matthew Saltzman mjs at clemson.edu
Thu May 24 14:00:30 UTC 2007


On Thu, 24 May 2007, Mike McCarty wrote:

> Matthew Saltzman wrote:
>> 
>> 
>> It will definitely print *something*.  The question is, can you guarantee 
>> what it will print.
>
> In C89, no in C99, yes.
>
>> It's not addressed directly in the FAQ, but I believe it's possible to 
>> prove that (unsigned) -2 must be the two's complement representation of -2 
>> in however many bits make up an int.  I know there was some controversy 
>> about that when the standard was being developed.  In any case, I don't 
>> know of any modern machine that doesn't represent negative integers in 
>> two's complement.
>
> Reach into your pocket, and pull out your calculator.

Fair enough.  I'll concede the point if you can name a standard-conforming 
C compiler for my calculator.

H&S note that sign-magnitude and one's complement are permitted, but the 
Usenet discussions involved subtle arguments that are long lost to (my) 
memory (as is the final conclusion).  For most of us programming 
modern general-purpose computers, it's a moot point.

H&S also state in an example that

 	unsigned u;

 	if ( u > -1 ) ...

will never execute the if block (because -1 is cast to unsigned before the 
comparison).  But that's not true for sign-magnitude.

>
> [snip]
>
>> 
>> sizeof(char) == 1 is guaranteed by the standard.  There's no reference to 
>> "bytes", but it is commonly accepted that the char type is a byte. 
>
> Erm, from the Standard:
>
> 3.4
>       [#1] byte
>       addressable  unit  of  data storage large enough to hold any
>       member  of  the  basic  character  set  of   the   execution
>       environment
>

OK that was from memory (and at 3am, to boot).  From H&S, a "byte" or 
"storage unit" is as above, essentially, the smallest independently 
addressible block of storage.  The char type has a size of one byte. (As I 
recall, whether a standard-conforming compiler could be written for a 
word-addressible machine was also a subject of language-lawyer debates on 
Usenet.)

>> It's possible to have chars that are not eight bits, but I can't think of a 
>> modern machine that does that.  There were some old machines (Honeywells?) 
>> that had six-bit bytes and 36-bit words.
>> 
>> All this is based on my recollection of discussions in comp.lang.c and 
>> comp.std.c when the standard was under development.
>> 
>
> Mike
>

-- 
 		Matthew Saltzman

Clemson University Math Sciences
mjs AT clemson DOT edu
http://www.math.clemson.edu/~mjs




More information about the fedora-list mailing list