Integer promotion of function parameters

Discussion in 'C Programming' started by =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire, Oct 22, 2007.

  1. 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
    --
    Jean-François Lemaire
    =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire, Oct 22, 2007
    #1
    1. Advertising

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

    Eric Sosman Guest

    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.


    --
    Eric Sosman, Oct 22, 2007
    #2
    1. Advertising

  3. 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. :)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 23, 2007
    #3
  4. 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.
    --
    Jean-François Lemaire
    =?ISO-8859-15?Q?Jean=2DFran=E7ois?= Lemaire, Oct 23, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray
    Replies:
    4
    Views:
    1,483
    Jack Klein
    Jan 6, 2004
  2. Russell Shaw

    Integer promotion and overflow

    Russell Shaw, Jan 2, 2005, in forum: C Programming
    Replies:
    20
    Views:
    1,076
    Keith Thompson
    Jan 3, 2005
  3. promotion of integer types

    , Mar 26, 2005, in forum: C Programming
    Replies:
    3
    Views:
    323
  4. Frederick Gotham

    Integer Promotion?

    Frederick Gotham, Jun 9, 2006, in forum: C Programming
    Replies:
    21
    Views:
    730
    Peter Boettcher
    Jun 28, 2006
  5. Replies:
    1
    Views:
    554
    christian.bau
    Jan 15, 2007
Loading...

Share This Page