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.


Relevant Pages

  • [C] #define - unexpected Results
    ... void foo; ... void foo(unsigned char a) ... I tried it out on my PC compiler and got 9 both times. ...
  • Re: how to make these warnings disappear?
    ... typedef unsigned char BYTE; ... Make sure type 'unsigned int' on your system is at least ... How can I get rid of these four warnings? ... They will often inhibit compiler ...
  • re-compile error
    ... void main ... char i,j; ... unsigned int CTRL_PORT,STAR_PORT; ... Compiler: Default compiler ...
  • Re: I am re-compile the source program under window XP
    ... int void main ... char i,j; ... unsigned int CTRL_PORT,STAR_PORT; ... Compiler: Default compiler ...
  • Re: Overloading of operator names - opinions sought
    ... possible, but it is more likely that the known cases will be known to the compiler implementer, and hence will be better optimized. ... char and bool are also special integer types. ... would either require some special stropping syntax, context dependence, ... with the realization that I would need multiple casts due to differences in semantics. ...