About Union's question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?gb2312?B?zfWzrLey?=

    #1

    About Union's question

    Union un
    { int I;
    char c[2];
    }

    main()
    {
    union un x;
    x.c[0]=10;
    x.c[1]=1;
    printf("%d",.i) ;
    }


    What is the output results? Please explain in detail under.

  • Joachim Schmitz

    #2
    Re: About Union's question

    "Íõ³¬·²" <wangchaofan@gm ail.comschrieb im Newsbeitrag
    news:1175235895 .997556.161250@ d57g2000hsg.goo glegroups.com.. .
    #include <stdio.h>
    Union un
    union un
    { int I;
    char c[2];
    }
    };
    >
    main()
    int main(void)
    {
    union un x;
    x.c[0]=10;
    x.c[1]=1;
    printf("%d",.i) ;
    printf("%d\n",x .I);
    return 0;
    }
    >
    >
    What is the output results? Please explain in detail under.
    Lots of compiler errors and some warnings too, without the modifications
    mentioned above. After having fixed these, on one of my machine the output
    was 167838688, on another it was 1073807626.
    Heavily depends in the implementation and machine architecture (big
    endian/little endian)

    Bye, Jojo


    Comment

    • jaysome

      #3
      Re: About Union's question

      On 29 Mar 2007 23:24:56 -0700, "??????" <wangchaofan@gm ail.comwrote:
      >Union un
      >{ int I;
      >char c[2];
      >}
      >
      >main()
      >{
      union un x;
      x.c[0]=10;
      x.c[1]=1;
      printf("%d",.i) ;
      >}
      >
      >
      >What is the output results? Please explain in detail under.
      That code does not even compile with my compilers and should not
      compile with yours. You should at least make an effort to insure that
      your code compiles before submitting it to this newsgroup. Otherwise,
      it can be difficult for anyone here to provide help to you.

      Best regards
      --
      jay

      Comment

      • =?utf-8?B?546L6LaF5Yeh?=

        #4
        Re: About Union's question

        On 3月30日, 下午3时46分 , "JoachimSchmitz " <nospam.schm... @hp.comwrote:
        "Íõ³¬· ²" <wangchao...@gm ail.comschrieb imNewsbeitragne ws:1175235895.9 97556.161250@d5 7g2000hsg.googl egroups.com...
        #include <stdio.h>Unio n un
        union un
        { int I;
        char c[2];
        }
        };
        >
        main()
        int main(void)
        {
        union un x;
        x.c[0]=10;
        x.c[1]=1;
        printf("%d",.i) ;
        >
        printf("%d\n",x .I);
        return 0;}
        >
        Thank you very much!I have something wrong with the procedure, as you
        corrected by the right .

        #include <stdio.h>
        union un
        {
        int I;
        char c[2];
        }


        int main(void)
        {
        union un x;
        x.c[0]=10;
        x.c[1]=1;
        printf("%d",.i) ;

        printf("%d\n",x .I);
        return 0;
        }
        What is the output results? Please explain in detail under.
        >
        Lots of compiler errors and some warnings too, without the modifications
        mentioned above. After having fixed these, on one of my machine the output
        was 167838688, on another it was 1073807626.
        Heavily depends in the implementation and machine architecture (big
        endian/little endian)
        >
        Bye, Jojo
        However, I still do not know how the compiler theory, Why the digital
        output ? Depends what the outcome is different for each output?



        Comment

        • Richard Heathfield

          #5
          Re: About Union's question

          ??? said:
          Union un
          { int I;
          char c[2];
          }
          >
          main()
          {
          union un x;
          x.c[0]=10;
          x.c[1]=1;
          printf("%d",.i) ;
          }
          >
          >
          What is the output results?
          It would be undefined because you're missing a header, except that it
          won't compile because you're missing an x. And when you've added the x,
          you still have to change i to I or I to i (either will do), and U to u.

          Once you've done all that, the answer is "it depends".

          All in all, I wouldn't bother if I were you.

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at the above domain, - www.

          Comment

          • jaysome

            #6
            Re: About Union's question

            On 30 Mar 2007 00:02:33 -0700, "???" <wangchaofan@gm ail.comwrote:
            >On 3?30?, ??3?46?, "Joachim Schmitz" <nospam.schm... @hp.comwrote:
            >"Íõ³¬·²" <wangchao...@gm ail.comschrieb im Newsbeitragnews :1175235895.997 556.161250@d57g 2000hsg.googleg roups.com...
            >#include <stdio.h>Unio n un
            >union un
            { int I;
            char c[2];
            }
            >};
            >>
            main()
            >int main(void)
            {
            union un x;
            x.c[0]=10;
            x.c[1]=1;
            printf("%d",.i) ;
            >>
            >printf("%d\n", x.I);
            >return 0;}
            >>
            >Thank you very much!I have something wrong with the procedure, as you
            >corrected by the right .
            Your code still does not compile!
            >#include <stdio.h>
            >union un
            >{
            >int I;
            >char c[2];
            }
            Change that to:

            };
            >
            >
            >int main(void)
            >{
            union un x;
            x.c[0]=10;
            x.c[1]=1;
            printf("%d",.i) ;
            That statement won't compile and requires a diagnostic. Just delete
            it, since your following statement is correct.
            >
            printf("%d\n",x .I);
            return 0;
            >}
            I notice that you changed your posted name from "?????" to "???". I
            have a feeling that if you change your posted name to simply "?", your
            code will compile :)

            All kidding aside, I'll tell you this ... in my 15+ years of
            programming in C, I have never--not once--legitimately used a union,
            because I've never needed to legitimately use one.

            I've dealt with unions declared by compilers, which most compilers
            inevitably seem to use (and I can live with that), and I've dealt with
            fellow programmers who've declared unions (IMHO, unnecessarily). But
            I've never found the need to legitimately use unions myself.

            My advice to you is to understand unions, but to never use them in
            your own code. In your example, I can tell you that you will not get
            the results you expect on a lot of implementations , such as those in
            which sizeof(int) != 2 (many if not most) and on those in which the
            Endianness is Big when yours is Little or vice versa.

            Best regards
            --
            jay

            Comment

            • Marcin

              #7
              Re: About Union's question

              Union un
              { int I;
              char c[2];
              }
              >
              After "fixing" your code to make it compiled:

              - int 4 bytes
              - char 1 byte, char[2] - 2 bytes

              so, after union is created it is filled with trash. Then you assign a values
              but only to "char" components, the other two bytes of "int" component are
              still filled with "trash". So final answer is "you will get unspecified
              number".


              Comment

              • Joachim Schmitz

                #8
                Re: About Union's question

                "Marcin" <bla@bla.plschr ieb im Newsbeitrag
                news:euij31$880 $1@atlantis.new s.tpi.pl...
                >Union un
                >{ int I;
                >char c[2];
                >}
                >>
                >
                After "fixing" your code to make it compiled:
                >
                - int 4 bytes
                - char 1 byte, char[2] - 2 bytes
                >
                so, after union is created it is filled with trash. Then you assign a
                values
                but only to "char" components, the other two bytes of "int" component are
                still filled with "trash". So final answer is "you will get unspecified
                number".
                Ah, yes, I missed that these other bytes still may be uninitialized.. . (on
                an implementation with sizeof(int) != 2)

                but
                union un x;
                x.I = 0;
                x.c[0]=10;
                x.c[1]=1;
                printf("%d",x.I );

                would still give some implementation/architecture dependant output.

                Bye, Jojo


                Comment

                • Richard Heathfield

                  #9
                  Re: About Union's question

                  Marcin said:

                  <snip>
                  After "fixing" [the] code to make it compiled:
                  >
                  - int 4 bytes
                  - char 1 byte, char[2] - 2 bytes
                  What makes you think int is 4 bytes? It might be only one byte, or it
                  might be two bytes, or it might even be ninety-seven bytes.

                  --
                  Richard Heathfield
                  "Usenet is a strange place" - dmr 29/7/1999

                  email: rjh at the above domain, - www.

                  Comment

                  • Army1987

                    #10
                    Re: About Union's question


                    "Richard Heathfield" <rjh@see.sig.in validha scritto nel messaggio
                    news:PdadnavcgZ i2d5HbnZ2dnUVZ8 vydnZ2d@bt.com. ..
                    What makes you think int is 4 bytes? It might be only one byte, or it
                    might be two bytes, or it might even be ninety-seven bytes.
                    It can't be one byte.
                    One byte is defined as the size of an unsigned char, i.e. CHAR_BIT bits.
                    All 2^CHAR_BIT = UCHAR_MAX + 1 possible sequences of CHAR_BIT bits must be
                    valid unsigned char representations . Also, CHAR_BIT is required to be at
                    least 8.

                    An int must be able to represent all possible values of unsigned char, so
                    INT_MAX >= UCHAR_MAX.
                    Also, it must be able to represent all negative numbers down to -INT_MAX.
                    So, it must be able to represent at least 2*UCHAR_MAX - 1 values. To do
                    that, it must have at least ceil(log2(2*UCH AR_MAX - 1)) = CHAR_BIT + 1 bits.

                    Since any object must occupy an integer number of bytes, an int must have at
                    least 2 bytes.


                    Comment

                    • Richard Heathfield

                      #11
                      Re: About Union's question

                      Army1987 said:
                      >
                      "Richard Heathfield" <rjh@see.sig.in validha scritto nel messaggio
                      news:PdadnavcgZ i2d5HbnZ2dnUVZ8 vydnZ2d@bt.com. ..
                      >What makes you think int is 4 bytes? It might be only one byte, or it
                      >might be two bytes, or it might even be ninety-seven bytes.
                      >
                      It can't be one byte.
                      Yes, it can.
                      One byte is defined as the size of an unsigned char, i.e. CHAR_BIT
                      bits. All 2^CHAR_BIT = UCHAR_MAX + 1 possible sequences of CHAR_BIT
                      bits must be valid unsigned char representations . Also, CHAR_BIT is
                      required to be at least 8.
                      All true.
                      An int must be able to represent all possible values of unsigned char,
                      Chapter and verse, please.

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: rjh at the above domain, - www.

                      Comment

                      • Richard Bos

                        #12
                        Re: About Union's question

                        "Army1987" <please.ask@for .itwrote:
                        "Richard Heathfield" <rjh@see.sig.in validha scritto nel messaggio
                        What makes you think int is 4 bytes? It might be only one byte, or it
                        might be two bytes, or it might even be ninety-seven bytes.
                        >
                        It can't be one byte.
                        One byte is defined as the size of an unsigned char, i.e. CHAR_BIT bits.
                        All 2^CHAR_BIT = UCHAR_MAX + 1 possible sequences of CHAR_BIT bits must be
                        valid unsigned char representations . Also, CHAR_BIT is required to be at
                        least 8.
                        >
                        An int must be able to represent all possible values of unsigned char, so
                        INT_MAX >= UCHAR_MAX.
                        Why?

                        Richard

                        Comment

                        • =?gb2312?B?zfWzrLey?=

                          #13
                          Re: About Union's question

                          On 3ÔÂ30ÈÕ, ÏÂÎç9ʱ37·Ö, "Army1987" <please....@for ..itwrote:
                          "Richard Heathfield" <r...@see.sig.i nvalidha scritto nel messaggionews:P dadnavcgZi2d5Hb nZ2dnUVZ8vydnZ2 d@bt.com...
                          >
                          What makes you think int is 4 bytes? It might be only one byte, or it
                          might be two bytes, or it might even be ninety-seven bytes.
                          >
                          It can't be one byte.
                          One byte is defined as the size of an unsigned char, i.e. CHAR_BIT bits.
                          All 2^CHAR_BIT = UCHAR_MAX + 1 possible sequences of CHAR_BIT bits mustbe
                          valid unsigned char representations . Also, CHAR_BIT is required to be at
                          least 8.
                          >
                          An int must be able to represent all possible values of unsigned char, so
                          INT_MAX >= UCHAR_MAX.
                          Also, it must be able to represent all negative numbers down to -INT_MAX.
                          So, it must be able to represent at least 2*UCHAR_MAX - 1 values. To do
                          that, it must have at least ceil(log2(2*UCH AR_MAX - 1)) = CHAR_BIT + 1 bits.
                          >
                          Since any object must occupy an integer number of bytes, an int must haveat
                          least 2 bytes.
                          It may seem very difficult and the things

                          Comment

                          • Army1987

                            #14
                            Re: About Union's question


                            "Richard Heathfield" <rjh@see.sig.in validha scritto nel messaggio
                            news:iLidnZXJEt K6gpDbRVnyjwA@b t.com...
                            Army1987 said:
                            >
                            >>
                            >"Richard Heathfield" <rjh@see.sig.in validha scritto nel messaggio
                            >news:Pdadnavcg Zi2d5HbnZ2dnUVZ 8vydnZ2d@bt.com ...
                            >>What makes you think int is 4 bytes? It might be only one byte, or it
                            >>might be two bytes, or it might even be ninety-seven bytes.
                            >>
                            >It can't be one byte.
                            >
                            Yes, it can.
                            >
                            >One byte is defined as the size of an unsigned char, i.e. CHAR_BIT
                            >bits. All 2^CHAR_BIT = UCHAR_MAX + 1 possible sequences of CHAR_BIT
                            >bits must be valid unsigned char representations . Also, CHAR_BIT is
                            >required to be at least 8.
                            >
                            All true.
                            >
                            >An int must be able to represent all possible values of unsigned char,
                            >
                            Chapter and verse, please.
                            What is the return type of getchar(), which can be any value from 0 to
                            UCHAR_MAX, or EOF, totalling 2^CHAR_BIT + 1 values?


                            Comment

                            • Richard Heathfield

                              #15
                              Re: About Union's question

                              Army1987 said:
                              >
                              "Richard Heathfield" <rjh@see.sig.in validha scritto nel messaggio
                              news:iLidnZXJEt K6gpDbRVnyjwA@b t.com...
                              >Army1987 said:
                              >>
                              <snip>
                              >>
                              >>An int must be able to represent all possible values of unsigned
                              >>char,
                              >>
                              >Chapter and verse, please.
                              >
                              What is the return type of getchar(), which can be any value from 0 to
                              UCHAR_MAX, or EOF, totalling 2^CHAR_BIT + 1 values?
                              The return type of getchar is of course int. Note that this int value
                              represents "an unsigned char converted to an int". So the conversion
                              rules apply. If ints are one byte in size, then if the value of the
                              unsigned char cannot be represented by the int, 3.2.1.2 applies:

                              "When an integer is demoted to a signed integer with smaller size, or an
                              unsigned integer is converted to its corresponding signed integer, if
                              the value cannot be represented the result is implementation-defined."

                              In C99, it's 6.3.1.3 (3), and the situation is slightly different:

                              "Otherwise, the new type is signed and the value cannot be represented
                              in it; either the result is implementation-defined or an
                              implementation-defined signal is raised."

                              In neither case (C90 or C99) is int required to be larger than one byte.
                              It must, of course, be at least 16 bits wide.

                              --
                              Richard Heathfield
                              "Usenet is a strange place" - dmr 29/7/1999

                              email: rjh at the above domain, - www.

                              Comment

                              Working...