Re: [C] #define - unexpected Results

From: Arthur J. O'Dwyer (
Date: 11/29/04

Date: Sun, 28 Nov 2004 19:36:01 -0500 (EST)

On Sun, 28 Nov 2004, Paulie wrote:
> I just came across something that I think is weird in my program and
> wondered if I was misunderstanding it or the compiler was at fault.

> void foo(unsigned char);
> #define NINE 9
> foo(NINE);
> void foo(unsigned char a) {
> printf("%x", a);
> }

> I would expect the program to print out 9 as the hex value, but it actually
> prints out F9

   The only Standard explanation of which I'm aware is that on your
machine, 'unsigned char' may promote to 'int', not 'unsigned int', and
thus you're invoking undefined behavior by passing that 'int' to 'printf'
(which is expecting an unsigned int, according to the "%x" specification).
Does the problem go away when you change the offending function to

   void foo(unsigned char a) {
     printf("%x", (unsigned int) a);


> (I am working on a mainframe system using EBCDIC notation so
> F9 is character '9').

   This ought to be irrelevant, given the program you've supplied us. The
character value '9' should never enter the picture here; you're dealing
only with the integral value 9.

   Post a complete, compilable program that illustrates the problem (i.e.,
one that produces incorrect results on your mainframe). The one you gave
didn't compile because of the "blah blah" lines.