Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!umich!sharkey!amara!mcdaniel From: mcdaniel@amara.uucp (Tim McDaniel) Newsgroups: comp.lang.c Subject: Re: A Simple question Message-ID: Date: 12 Apr 90 23:56:55 GMT References: <1524@mountn.dec.com> Sender: news@amara.UUCP Organization: Applied Dynamics Int'l. Lines: 30 In-reply-to: wallis@labc.dec.com's message of 11 Apr 90 05:25:24 GMT yhe@zip.eecs.umich.edu (Youda He) asked about "printf("a=%X\n",a);", where "a" is a signed char. wallis@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@mailgw.cc.umich.edu UUCP: {uunet,sharkey}!amara!mcdaniel