ECE-1021

Common Fallacies Concerning C

(Last Mod: 27 November 2010 21:38:36 )

ECE-1021 Home


  1. FALLACY: Negative integers are represented using two's compliment.

    The C Standard actually allows the compiler designer to choose from among three different representations - two's complement, signed binary, and one's compliment.

     

  2. FALLACY: All bits in an integer are utilized.

    The C Standard allows the representation to contain unused padding bits. The only integer data type required to use all of its bits is the unsigned char data type.

     

  3. FALLACY: All data types are a multiple of eight bits.

    The C Standard requires that the size of a char (plain, signed, or unsigned) be exactly CHAR_BIT bits wide. This is a constant defined in stddef.h. All other data types must be an integer multiple of CHAR_BIT. The value of CHAR_BIT must be no less than eight, but may be more.

     

  4. FALLACY: Characters are stored in ASCII.

    The C Standard makes now such requirement or assumption. Certain characters are required to be contained in the character set and a few other constraints are imposed. Beyond that it is up to the compiler writer.

     

  5. FALLACY: We can use bitwise operations to get at the internal representation of floating point values:

    The C Standard requires that bitwise operations use only integer operands - the use of a non-integer operand is not undefined behavior, it is a constraint violation that will not compile.

     

  6. FALLACY: We can get around the limitation that bitwise operators require integer operands by type casting any floating point values.

    We can do this - and the code will compile and run - but if our intent was to get at the internal representation of a floating point data type we have completely defeated the purpose since the type cast retains the value (as best it can) and changes the representation before passing it to the operator.

     

  7. FALLACY: The bit shift operators always shift a zero into the vacated bit.

    This is true for the left-shift operator. The right shift operator will shift in a zero for unsigned data types and for signed data types if the value stored non-negative. If the value is negative, the behavior is implementation-defined.

     

  8. FALLACY: A "plain" char is the same as a signed char.

    This is up to the compiler designer. It can either be a signed or an unsigned char, at their discretion.

     

  9. FALLACY: A "plain" char is the same as an unsigned char.

    This is up to the compiler designer. It can either be a signed or an unsigned char, at their discretion.