A Simple question

Tim McDaniel mcdaniel at amara.uucp
Fri Apr 13 09:56:55 AEST 1990


yhe at zip.eecs.umich.edu (Youda He) asked about "printf("a=%X\n",a);",
where "a" is a signed char.

wallis at labc.dec.com (Barry L. Wallis) replies:

   The %X specifier in printf() tells the function to interpret the
   argument as an int.  Since the actual value is a char it is pushed
   on the stack as an int.

Those statements, in that order, may be ambiguous.  (As I first read
them, they seemed to say that printf does the type conversion after
seeing %X!)  To amplify:

Generally, an actual argument to a function undergoes "argument
promotion".  For example, integral type arguments smaller than an
"int" are automatically and silently converted to "int".  (The
compiler emits code to do this---the smaller-type values never reach
the function.)  The variable "a", with value -1, is converted to an
"int" with value -1.  printf (on the machine in question) prints an
int -1 as "FFFF".

ANSI C provides function prototypes, which allows you to avoid
argument promotion in most cases.  However, printf has a variable
number of arguments, and prototypes can't help here.

--
Tim McDaniel
Applied Dynamics International, Ann Arbor, MI
Internet: mcdaniel%amara.uucp at mailgw.cc.umich.edu
UUCP: {uunet,sharkey}!amara!mcdaniel



More information about the Comp.lang.c mailing list