Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About Union's question

Reply
Thread Tools

About Union's question

 
 
=?gb2312?B?zfWzrLey?=
Guest
Posts: n/a
 
      03-30-2007
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.

 
Reply With Quote
 
 
 
 
jaysome
Guest
Posts: n/a
 
      03-30-2007
On 29 Mar 2007 23:24:56 -0700, "??????" <> wrote:

>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
 
Reply With Quote
 
 
 
 
=?utf-8?B?546L6LaF5Yeh?=
Guest
Posts: n/a
 
      03-30-2007
On 3月30日, 下午3时46分, "Joachim Schmitz" <nospam.schm...@hp.com> wrote:
> "Íõ³¬·²" <wangchao...@gmail.com> schrieb im Newsbeitragnews:1175235895.997556.161250@d57g2000h sg.googlegroups.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;> }
>

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?



 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-30-2007
"" <> schrieb im Newsbeitrag
news: oups.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


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-30-2007
??? 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
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      03-30-2007
On 30 Mar 2007 00:02:33 -0700, "???" <> wrote:

>On 3?30?, ??3?46?, "Joachim Schmitz" <nospam.schm...@hp.com> wrote:
>> "" <wangchao...@gmail.com> schrieb im Newsbeitragnews:1175235895.997556.161250@d57g2000h sg.googlegroups.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;> }
>>

>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
 
Reply With Quote
 
Marcin
Guest
Posts: n/a
 
      03-30-2007
> 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".


 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-30-2007
"Marcin" <> schrieb im Newsbeitrag
news:euij31$880$...
>> 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


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-30-2007
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
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      03-30-2007

"Richard Heathfield" <> ha scritto nel messaggio
news...
> 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*UCHAR_MAX - 1)) = CHAR_BIT + 1 bits.

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57