Programming in C

(Barry) #1
Data Type Conversions 329

operator. Because one of the operands of the division would then be of type float, the
other (s) would be automatically converted to type float, and that would be the type of
the result.


Sign Extension


Whenever a signed intor signed short intis converted into an integer of a larger
size, the sign is extended to the left when the conversion is performed.This ensures that
a short inthaving a value of –5,for example, will also have the value –5when con-
verted to a long int.Whenever an unsigned integer is converted to an integer of a larg-
er size, as you would expect, no sign extension occurs.
On some systems (such as Mac G4/G5 and Pentium processors) characters are treated
as signed quantities.This means that when a character is converted to an integer, sign
extension occurs. As long as characters are used from the standard ASCII character set,
this fact will never pose a problem. However, if a character value is used that is not part
of the standard character set, its sign might be extended when converted to an integer.
For example on a Mac, the character constant ‘\377’is converted to the value –1
because its value is negative when treated as a signed, eight-bit quantity.
Recall that the C language permits character variables to be declared unsigned, thus
avoiding this potential problem.That is, an unsigned charvariable will never have its
sign extended when converted to an integer; its value will always be greater than or
equal to 0 .For the typical eight-bit character, a signed character variable, therefore, has
the range of values from -128to +127, inclusive. An unsigned character variable can
range in value from 0 to 255 , inclusive.
If you want to force sign extension on your character variables, you can declare such
variables to be of type signed char.This ensures that sign extension will occur when
the character value is converted to an integer, even on machines that don’t do so by
default.


Argument Conversion


You have used prototype declarations for all the functions that you have written in this
book. In Chapter 8, “Working with Functions,” you learned this was prudent because
you can physically locate the function either before or after its call, or even in another
source file, with a prototype declaration. It was also noted that the compiler automatical-
ly converts your arguments to the appropriate types as long as it knows the types of
arguments the function expects.The only way it can know this is by having previously
encountered the actual function definition or a prototype declaration.
Recall that, if the compiler sees neither the function definition nor a prototype decla-
ration before it encounters a call to a function, it assumes the function returns an int.
The compiler also makes assumptions about its argument types. In the absence of infor-
mation about the argument types to a function, the compiler automatically converts
_Bool,char, or shortarguments to ints and converts floatarguments to double.

Free download pdf