type casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roman Mashak

    #1

    type casting

    Hello, All!

    Given the sample piece of code I have:

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    short int i, j;
    int k;
    i = j = 10;
    k = i + j;

    return 0;
    }

    So, here I expect that 'i' and 'j' will be type-casted to 'int' type, while
    in debugger (exploring memory dump) I see these variables still occupy 2
    bytes (i.e. 'short' type).

    Is it normal behavior? I suppose, I just don't catch some point.

    With best regards, Roman Mashak. E-mail: mrv@tusur.ru


  • Richard Bos

    #2
    Re: type casting

    "Roman Mashak" <mrv@tusur.ru > wrote:
    [color=blue]
    > #include <stdio.h>
    > #include <string.h>[/color]

    Two superfluous headers; you use nothing from either.
    [color=blue]
    > int main(void)
    > {
    > short int i, j;
    > int k;
    > i = j = 10;
    > k = i + j;
    >
    > return 0;
    > }
    >
    > So, here I expect that 'i' and 'j' will be type-casted to 'int' type,[/color]

    Why on earth would you expect that? There is not a single cast in your
    program.
    Even supposing you mean converted rather than cast, yes, their values
    will, but only temporarily during the assignments. This has no influence
    on their actual sizes as objects.
    [color=blue]
    > while in debugger (exploring memory dump) I see these variables still
    > occupy 2 bytes (i.e. 'short' type).[/color]

    Of course. You have explicitly asked for them to do so.

    That _the values of_ these shorts will then be converted to int before
    being added to one another and/or assigned to other objects (in the
    first case, being converted back to short before the assignment),
    doesn't change the size of the objects themselves.

    Richard

    Comment

    • Zoran Cutura

      #3
      Re: type casting

      Roman Mashak <mrv@tusur.ru > wrote:[color=blue]
      > Hello, All!
      >
      > Given the sample piece of code I have:
      >
      > #include <stdio.h>
      > #include <string.h>
      >
      > int main(void)
      > {
      > short int i, j;
      > int k;
      > i = j = 10;
      > k = i + j;
      >
      > return 0;
      > }
      >
      > So, here I expect that 'i' and 'j' will be type-casted to 'int' type, while
      > in debugger (exploring memory dump) I see these variables still occupy 2
      > bytes (i.e. 'short' type).[/color]

      Note that the term "type-cast" is used for explicit conversion of types
      by usage of the so called cast operators. What you'ld expect was
      probably a conversion.

      It's not the variables themselve that would get converted, but the
      operands of the operation. Here even the operands do not have to be
      converted because i and j are of the same integer type and the
      + operator doesn't require any special conversion.

      So if you do something along

      i = 18888;
      j = 22222;
      k = i+j;

      You may get undefined behavior (supposing that SHRT_MAX is 32767).
      Even though k is capable of holding the result, the operation will not
      be done in the type of k, but with the type of i and j.

      Also note that explicit type casts do not change the size of a variable.
      A type cast is an operator jsut like + or *, and has a result. The type
      of the result is that specified by the cast operator.

      --
      Z (zoran.cutura@w eb.de)
      "LISP is worth learning for the profound enlightenment experience
      you will have when you finally get it; that experience will make you
      a better programmer for the rest of your days." -- Eric S. Raymond

      Comment

      • Roman Mashak

        #4
        Re: type casting

        Hello, Richard!
        You wrote on Tue, 23 Aug 2005 06:47:33 GMT:

        RB> Why on earth would you expect that? There is not a single cast in your
        RB> program.
        RB> Even supposing you mean converted rather than cast, yes, their values
        RB> will, but only temporarily during the assignments. This has no
        RB> influence on their actual sizes as objects.
        Oops, I missed the terms. Converting != casting.
        ??>> while in debugger (exploring memory dump) I see these variables still
        ??>> occupy 2 bytes (i.e. 'short' type).

        RB> Of course. You have explicitly asked for them to do so.

        RB> That _the values of_ these shorts will then be converted to int before
        RB> being added to one another and/or assigned to other objects (in the
        RB> first case, being converted back to short before the assignment),
        RB> doesn't change the size of the objects themselves.
        Ok, I understood. In case of implicit type-casting - will it keep the object
        size in memory ?

        With best regards, Roman Mashak. E-mail: mrv@tusur.ru


        Comment

        • Michael Mair

          #5
          Re: type casting

          Roman Mashak wrote:[color=blue]
          > Hello, Richard!
          > You wrote on Tue, 23 Aug 2005 06:47:33 GMT:
          >
          > RB> Why on earth would you expect that? There is not a single cast in your
          > RB> program.
          > RB> Even supposing you mean converted rather than cast, yes, their values
          > RB> will, but only temporarily during the assignments. This has no
          > RB> influence on their actual sizes as objects.
          > Oops, I missed the terms. Converting != casting.
          > ??>> while in debugger (exploring memory dump) I see these variables still
          > ??>> occupy 2 bytes (i.e. 'short' type).
          >
          > RB> Of course. You have explicitly asked for them to do so.
          >
          > RB> That _the values of_ these shorts will then be converted to int before
          > RB> being added to one another and/or assigned to other objects (in the
          > RB> first case, being converted back to short before the assignment),
          > RB> doesn't change the size of the objects themselves.
          > Ok, I understood. In case of implicit type-casting - will it keep the object
          > size in memory ?[/color]

          There is no implicit typecasting. You either cast or you don't.
          Conversions can happen implicitly (without you doing anything about it)
          or explicitly (with you casting).
          Reread Richard's answer.

          -Michael[color=blue]
          >
          > With best regards, Roman Mashak. E-mail: mrv@tusur.ru
          >
          >[/color]


          --
          E-Mail: Mine is an /at/ gmx /dot/ de address.

          Comment

          • Christian Kandeler

            #6
            Re: type casting

            Zoran Cutura wrote:
            [color=blue]
            > It's not the variables themselve that would get converted, but the
            > operands of the operation. Here even the operands do not have to be
            > converted because i and j are of the same integer type and the
            > + operator doesn't require any special conversion.[/color]

            It does require the Usual Arithmetic Conversions, which include integer
            promotion. So i and j _do_ get promoted to int before the addition takes
            place.


            Christian

            Comment

            • Zoran Cutura

              #7
              Re: type casting

              Christian Kandeler <christian.kand eler@hob.de_inv alid> wrote:[color=blue]
              > Zoran Cutura wrote:
              >[color=green]
              >> It's not the variables themselve that would get converted, but the
              >> operands of the operation. Here even the operands do not have to be
              >> converted because i and j are of the same integer type and the
              >> + operator doesn't require any special conversion.[/color]
              >
              > It does require the Usual Arithmetic Conversions, which include integer
              > promotion. So i and j _do_ get promoted to int before the addition takes
              > place.[/color]


              uuhuu ... I stand corrected. My brain seems to shrink an leave holes all
              over.

              Sorry for the incorrect explanation.

              --
              Z (zoran.cutura@w eb.de)
              "LISP is worth learning for the profound enlightenment experience
              you will have when you finally get it; that experience will make you
              a better programmer for the rest of your days." -- Eric S. Raymond

              Comment

              • pete

                #8
                Re: type casting

                Michael Mair wrote:[color=blue]
                >
                > Roman Mashak wrote:[/color]
                [color=blue][color=green]
                > > Ok, I understood. In case of implicit type-casting
                > > - will it keep the object size in memory ?[/color]
                >
                > There is no implicit typecasting. You either cast or you don't.[/color]

                Typecasting is something that happens to thespians.

                http://www.m-w.com/cgi-bin/dictionary?va=typecasting

                In C, a cast, is just called a "cast"

                --
                pete

                Comment

                • pete

                  #9
                  Re: type casting

                  Zoran Cutura wrote:
                  [color=blue]
                  > Note that the term "type-cast"
                  > is used for explicit conversion of types[/color]

                  No, it isn't.
                  [color=blue]
                  > A type cast is an operator jsut like + or *,
                  > and has a result.[/color]

                  It's just a "cast", not a "type cast".

                  There is no other kind of cast in C, such that the word "cast"
                  needs an adjective in front of it to distinguish it.

                  --
                  pete

                  Comment

                  • Michael Mair

                    #10
                    Re: type casting

                    pete wrote:[color=blue]
                    > Michael Mair wrote:
                    >[color=green]
                    >>Roman Mashak wrote:[/color]
                    >
                    >[color=green][color=darkred]
                    >>>Ok, I understood. In case of implicit type-casting
                    >>>- will it keep the object size in memory ?[/color]
                    >>
                    >>There is no implicit typecasting. You either cast or you don't.[/color]
                    >
                    >
                    > Typecasting is something that happens to thespians.
                    >
                    > http://www.m-w.com/cgi-bin/dictionary?va=typecasting
                    >
                    > In C, a cast, is just called a "cast"[/color]

                    Argh. I was not aware of this -- seemingly there are enough like
                    me out there:

                    I don't know where I picked it up but I guess it was around here;
                    so I use "to cast" for the action and "typecastin g" as noun.
                    However, thank you :-)

                    Cheers
                    Michael
                    --
                    E-Mail: Mine is an /at/ gmx /dot/ de address.

                    Comment

                    Working...