INF: Signed char Type Converted to int Type at Function Call

ID Number: Q38025

4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

Any char value equal to or greater than 0x80 is printed with 0xff

preceding it when the following are done:

1. An array of char is declared and initialize it with hexadecimals.

2. Later in the program, printf() is used to display the values of

the array element.

3. "%2x" is used as a format string. (See sample program below.)

More Information:

The "unsigned char" should be used to declare the array "bit", or use

the /J compiler option to change the char type from its default of

signed char to unsigned char.

This is not a problem with the Microsoft C Compiler. In the sample

program below, the element of array "bits" has char type, which is a

signed type in the Microsoft C compiler. In C, when a variable is used

as an actual argument in a function call, usual unary data-type

conversion is automatically performed before the argument is passed to

the function.

In particular, signed char type is converted to int type using sign

extension (see the "Microsoft C Optimizing Compiler Language

Reference" manual for data conversion rules.) Because signed char type

can represent values from -128 to 127 (in our compiler -128 is not

defined), and a negative value is represented in two's complement

form, any hexadecimal number greater than 0x80, which is a negative

value in signed char type, will be converted to signed int type in

corresponding two's complement form.

For example, 0x80 (-128) is converted to 0xff80; 0x81 (-127) is

converted to 0xff81.

When using "printf" with unsigned hexadecimal integer control

character "%x", the values are displayed in its unsigned hexadecimal

format. If "%d" is used, the values are displayed in signed decimal

format.

The following is a sample program:

Sample Code

-----------

#include <stdio.h>

char bits[8] = {0x80, 0x81, 0x91, 0x00, 0x7f, 0x20, 0x40, 0x08} ;

main()

{ int i ;

for (i=0; i<8 ; i++)

printf("%2x ", bits[i]) ;

printf("\n") ;

for (i=0; i<8 ; i++)

printf("%d ", bits[i]) ;

}

Output :

ff80 ff81 ff91 00 7f 20 08

-128 -127 -111 0 127 32 8

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00