Int promotion

Discussion in 'C Programming' started by fasf, Dec 27, 2009.

  1. fasf

    fasf Guest

    Hi,
    I'm reading about int promotion: char and short shoud be avoided
    because are changed in int variables during arithmetic operations...So
    i shoud always avoid char and short variables? These variables are
    smaller in byte size, but new instructions (like sign extending) are
    implemented at compiler time: these instructions occupy memory bytes
    so (apparently) there is no advantage to use short data type....or
    not?
    fasf, Dec 27, 2009
    #1
    1. Advertising

  2. fasf

    Flash Gordon Guest

    fasf wrote:
    > Hi,
    > I'm reading about int promotion: char and short shoud be avoided


    That is an exaggeration. They should be used when appropriate and not
    when they are not appropriate.

    > because are changed in int variables during arithmetic operations...


    They get promoted, yes.

    >So
    > i shoud always avoid char and short variables?


    No.

    > These variables are
    > smaller in byte size, but new instructions (like sign extending) are
    > implemented at compiler time: these instructions occupy memory bytes
    > so (apparently) there is no advantage to use short data type....or
    > not?


    On some processors sign extension is a "free" operation, i.e. one that
    takes no additional time and can be done on loading the value in to the
    register where it is needed.

    Also, sometimes you have a large array of data, in which case the saving
    in space can make a big difference and even improve performance.

    Finally consider this. Why were the types included in the language if
    they were of no use?
    --
    Flash Gordon
    Flash Gordon, Dec 27, 2009
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    fasf wrote:

    > Hi,
    > I'm reading about int promotion: char and short shoud be avoided
    > because are changed in int variables during arithmetic operations...So
    > i shoud always avoid char and short variables? These variables are
    > smaller in byte size, but new instructions (like sign extending) are
    > implemented at compiler time: these instructions occupy memory bytes
    > so (apparently) there is no advantage to use short data type....or
    > not?


    For arithmetic operation, everything less than an `int' is converted to
    either `int' or `unsigned int', depending whether the source type is a
    subset of `int'. In theory, these operation should be performed on type
    `int' but an optimizing compiler can omit these conversions when
    appropriate.

    Consider the following header:

    size_t printf(const char *fmt, ...);

    When passing `char's and `short's to the variable arguments part, they will
    be promoted to `int's prior to passing. Therefore, when you try to read the
    arguments, you need to take an `int' out when a `char' or `short' is
    expected. Similar for `float' and `double'. However, This promotion does not
    occur when a function prototype is in effect. Instead, the argument is
    convert to the type of the function prototype.

    Consider the following pair of old-style function declaration and definition
    (very old K&R C without the void keyword):

    main(argc, argv)
    int argc;
    char **argv;
    {
    unsigned char x = 0;
    foo(x); /* foo is implicitly declared as `int foo(...)' */
    return 0;
    }

    foo(n)
    unsigned char n;
    {
    // blah
    }

    Here, n is converted from a `unsigned char' to an `int' (possibly `unsigned
    int' if `char' is the same size of `int') in main(), passed into foo, and
    converted back to `unsigned char'. To modify the declaration into prototype
    form, you should write this (standard C90):

    int foo(int);

    int main()
    int argc;
    char **argv;
    {
    unsigned char x = 0;
    foo(x);
    return 0;
    }

    int foo(n)
    unsigned char n;
    {
    // blah
    }

    Later, to convert the definition to prototype form:

    int foo(int);

    int main(int argc, char **argv)
    {
    unsigned char x = 0;
    foo(x);
    return 0;
    }

    int foo(int _n)
    {
    unsigned char n = _n;
    // blah
    }

    In general, although some CPU does provide byte/short operation directly, it
    is better not using them unless memory is very tight.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEARECAAYFAks3Z0kACgkQG6NzcAXitM+rIQCfSPqKhya1RWQCIppE/+px4+pw
    AjwAnAvwS0SyKAjCTP0V3J3QqqMc+CxK
    =638p
    -----END PGP SIGNATURE-----
    Michael Tsang, Dec 27, 2009
    #3
  4. fasf

    Eric Sosman Guest

    On 12/27/2009 7:53 AM, Flash Gordon wrote:
    > fasf wrote:
    >> Hi,
    >> I'm reading about int promotion: char and short shoud be avoided

    > [... good response snipped ...]
    > Finally consider this. Why were the types included in the language if
    > they were of no use?


    To keep gets() company?

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 27, 2009
    #4
  5. fasf

    Flash Gordon Guest

    Eric Sosman wrote:
    > On 12/27/2009 7:53 AM, Flash Gordon wrote:
    >> fasf wrote:
    >>> Hi,
    >>> I'm reading about int promotion: char and short shoud be avoided

    >> [... good response snipped ...]
    >> Finally consider this. Why were the types included in the language if
    >> they were of no use?

    >
    > To keep gets() company?


    If getc is get a char, gets should be get a short ;-)
    --
    Flash Gordon
    Flash Gordon, Dec 27, 2009
    #5
  6. fasf

    Eric Sosman Guest

    [OT] Re: Int promotion

    On 12/27/2009 2:23 PM, Richard Heathfield wrote:
    > Flash Gordon wrote:
    >> Eric Sosman wrote:
    >>> On 12/27/2009 7:53 AM, Flash Gordon wrote:
    >>>> fasf wrote:
    >>>>> Hi,
    >>>>> I'm reading about int promotion: char and short shoud be avoided
    >>>> [... good response snipped ...]
    >>>> Finally consider this. Why were the types included in the language if
    >>>> they were of no use?
    >>>
    >>> To keep gets() company?

    >>
    >> If getc is get a char, gets should be get a short ;-)

    >
    >
    > But getc is get an int! So gets should be get a long.


    Can't we all just get a long?

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 27, 2009
    #6
  7. fasf

    Flash Gordon Guest

    Richard Heathfield wrote:
    > Flash Gordon wrote:
    >> Eric Sosman wrote:
    >>> On 12/27/2009 7:53 AM, Flash Gordon wrote:
    >>>> fasf wrote:
    >>>>> Hi,
    >>>>> I'm reading about int promotion: char and short shoud be avoided
    >>>> [... good response snipped ...]
    >>>> Finally consider this. Why were the types included in the language if
    >>>> they were of no use?
    >>>
    >>> To keep gets() company?

    >>
    >> If getc is get a char, gets should be get a short ;-)

    >
    > But getc is get an int! So gets should be get a long.


    No, it gets a char (interpreted as an unsigned char), it just returns it
    in an int... so gets should get a short interpreted as an unsigned short :)
    --
    Flash Gordon
    Flash Gordon, Dec 27, 2009
    #7
  8. On Dec 27, 8:18 am, pete <> wrote:

    > Also, if you declare a pair of low ranking type objects
    > and if you can see how far apart their addresses are,
    > it is not unusual for them to be sizeof(int) bytes apart,
    > meaning that the bytes saved in between them, are useless.
    >
    > --
    > pete


    On my 64 bit system,
    [frodo@fedora c]$ cat shorts.c
    #include <stdio.h>

    int main(void)
    {
    char a, b;
    printf("\n%p, %p\n", &a, &b);
    return 0;
    }
    [frodo@fedora c]$ ./a.out

    0x7fff529dd89f, 0x7fff529dd89e
    [frodo@fedora c]$ uname -a
    Linux fedora 2.6.31.9-174.fc12.x86_64 #1 SMP Mon Dec 21 05:33:33 UTC
    2009 x86_64 x86_64 x86_64 GNU/Linux

    Regards,
    Frodo B
    Frodo Baggins, Dec 28, 2009
    #8
  9. fasf

    Nobody Guest

    On Sun, 27 Dec 2009 03:36:00 -0800, fasf wrote:

    > I'm reading about int promotion: char and short shoud be avoided because
    > are changed in int variables during arithmetic operations...So i shoud
    > always avoid char and short variables? These variables are smaller in byte
    > size, but new instructions (like sign extending) are implemented at
    > compiler time: these instructions occupy memory bytes so (apparently)
    > there is no advantage to use short data type....or not?


    Using individual char/short variables doesn't save much memory, and may
    not even save any; the compiler may just use the unused bytes as padding
    so that following variables are aligned.

    OTOH, the compiler may have to add extra instructions to separate the
    data from the padding (e.g. the ARM can read an 8-bit byte or 32-bit word;
    to read a 16-bit short, the compiler must read a 32-bit word then
    explicitly mask out the unused bits).

    Unless you're programming a microcontroller with miniscule amounts of RAM,
    the memory savings are unlikely to be worth it for individual variables.
    OTOH, it may be worthwhile for a large array of char/short, or a large
    array of structures with char/short fields.
    Nobody, Dec 29, 2009
    #9
    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. Chris
    Replies:
    8
    Views:
    2,562
    Michael Borgwardt
    Sep 4, 2004
  2. Timothy Madden
    Replies:
    4
    Views:
    372
    Timothy Madden
    Sep 28, 2004
  3. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
  5. gen_tricomi
    Replies:
    4
    Views:
    300
    gen_tricomi
    May 23, 2006
Loading...

Share This Page