Integer promotion of function parameters

  • Thread starter =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire
  • Start date
?

=?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire

Hello,

I'm having a discussion with someone who sustains that function
parameters, when they are smaller than an int (say short or char) are
automatically promoted to int before being passed to the caller.

Despite looking in the Standard (ISO/IEC 9899:TC2), &6.5.2.2 "Function
calls", I'm still unsure, because this is rather technical reading.

Can someone enlighten me?

Thanks,

JFL
 
E

Eric Sosman

Jean-François Lemaire wrote On 10/22/07 17:18,:
Hello,

I'm having a discussion with someone who sustains that function
parameters, when they are smaller than an int (say short or char) are
automatically promoted to int before being passed to the caller.

Despite looking in the Standard (ISO/IEC 9899:TC2), &6.5.2.2 "Function
calls", I'm still unsure, because this is rather technical reading.

Can someone enlighten me?

Three paragraphs seem important:

- 6.5.2.2p6 defines the "default argument promotions"
and describes when they are used.

- 6.5.2.2p7 describes when the default argument promotions
are *not* used, and what happens instead.

- 6.3.1.1p2 defines the "integer promotions," which make
up part of the default argument promotions.

In ordinary language: Your friend is right, but only
when prototypes are not used or for arguments that match
the `...' of a variable parameter list. Different rules
apply when a prototype supplies information about the type
of the parameter corresponding to an argument.
 
R

Richard Heathfield

Jean-François Lemaire said:
Hello,

I'm having a discussion with someone who sustains that function
parameters, when they are smaller than an int (say short or char) are
automatically promoted to int before being passed to the caller.

Despite looking in the Standard (ISO/IEC 9899:TC2), &6.5.2.2 "Function
calls", I'm still unsure, because this is rather technical reading.

Can someone enlighten me?

"If the expression that denotes the called function has a type that does
not include a prototype, the integral promotions are performed on each
argument and arguments that have type float are promoted to double. These
are called the default argument promotions."

"Integral promotions" means that char, short, int bit-fields, in both
signed and unsigned flavours, and enums, can all be used in an expression
wherever an int or unsigned int can be used, and in fact *are* so used,
with the promotion being to int if an int can represent all values of the
original type, or to unsigned int otherwise.

So yes, ***in the absence of a function prototype***, char and short are
certainly promoted either to int or unsigned int when used as, or in,
function arguments. In the presence of a function prototype, I think you
need to talk to someone like David, Eric, Dik or Chris. :)
 
?

=?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire

On Tuesday 23 October 2007 01:10, Richard Heathfield wrote:

[...]
So yes, ***in the absence of a function prototype***, char and short
are certainly promoted either to int or unsigned int when used as, or
in, function arguments. In the presence of a function prototype, I
think you
need to talk to someone like David, Eric, Dik or Chris. :)

This was a somewhat rhetorical question anyway, so your answer and Eric
Sosman's are more than adequate, particularly since they confirm my own
understanding of the Standard :)

Thanks to both of you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top