Re: Printf question.



Charles Richmond wrote:
usenet@xxxxxxx wrote:

praeiji <spacecaps@xxxxxxxxx> wrote:


But why are they typecasted to int? Does it come from gcc when it sees
"%d" and the printf function?

No, they are not typecasted, but `promoted', which is not the same thing. Promotion is what happens when you call a function that has no prototype, or when a function with a variable number (variadic) of arguments like printf() is called. With promotion, all arguments are converted to a generic 'bigger' type: char and short to int, float to double.


Type promotion can also happen when you do mixed mode arithmetic.

Or even unmixed-mode arithmetic:

	short x = 1, y = 2;
	short z = x + y;    /* see the "int"? */

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • 6.5.2.2p6, var args, and undefined behavior question
    ... prototype, the integer promotions are performed on each argument, and ... promotion are not compatible with the types of the parameters, ... void func(int x, ... ...
    (comp.std.c)
  • Re: printf length modifier
    ... The standard guarantees that the representation of all positive values ... of int is the same as the representation of those values in an unsigned ... does not include a prototype, the integer promotions are performed on ... and the types of the arguments after promotion ...
    (comp.lang.c)
  • Re: 6.5.2.2p6, var args, and undefined behavior question
    ... does not include a prototype, the integer promotions are performed on ... and the types of the arguments after promotion ... visible prototype, as long as there's a declaration ... The intent is that the condition "If the expression that denotes the ...
    (comp.std.c)
  • [PATCH]: type promotion prototypes fixes - NDIS
    ... the default promotion to int is not satisfied. ... the uint16_t x in the K&R function is promoted to int so the prototype ... most of the fixes in the patch does not change ABI but the NDIS does, ...
    (freebsd-current)
  • Re: type names
    ... operand is promoted to type int before the call). ... In the presence of a prototype, the argument is converted directly to the type of the parameter, as if by assignment. ... Even if all function arguments were promoted in C, rules for resolving overloaded functions could still refer to the types before promotion. ...
    (comp.std.c)