 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Mar 01, 2007 8:36 am Post subject: What's the output |
|
|
int main()
{
int k;
union jatin{
int i :5;
char j :2;
};
union jatin rajpal;
k= sizeof(rajpal);
printf("%d",k);
return 0;
}
& what would be the output if instead of union in the above example if
i'll use struct? Could anyon can explain me union behavior.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Tue Mar 06, 2007 2:41 am Post subject: Re: What's the output |
|
|
rajpal_jatin (AT) yahoo (DOT) co.in writes:
| Quote: | int main()
{
int k;
union jatin{
int i :5;
char j :2;
};
union jatin rajpal;
k= sizeof(rajpal);
printf("%d",k);
return 0;
}
& what would be the output if instead of union in the above example if
i'll use struct? Could anyon can explain me union behavior.
|
Your program invokes undefined behavior. It calls printf() with no
prototype in scope, which can be corrected by adding
#include <stdio.h>
In practice you're likely to get away with that, but it's a bad habit.
The output is the size, in bytes, of the object "rajpal". The
specific size is going to vary from one implementation to another. If
you use a struct rather than a union, then again, the specific size is
going to vary from one implementation to another.
Section 2 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has some
good information about structs an unions. If you're still confused
after reading that, try asking a more specific question. What in
particular surprises you about the behavior you're seeing?
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Tue Mar 06, 2007 2:41 am Post subject: Re: What's the output |
|
|
<rajpal_jatin (AT) yahoo (DOT) co.in> wrote...
| Quote: | int main()
{
int k;
union jatin{
int i :5;
char j :2;
};
union jatin rajpal;
k= sizeof(rajpal);
printf("%d",k);
return 0;
}
& what would be the output if instead of union in the above example if
i'll use struct? Could anyon can explain me union behavior.
|
A struct collects its members into a contiguous object, the first member
starting at the same place as the whole struct, the second object starting
just after the first object, etc. (There can be some padding bytes when
needed to align the member addresses suitably for their types.)
A union is just like a struct except that every member starts at the same
location; they are "overlaid", and evidently only one member can be
valid at a given moment.
As to what your program prints, it depends on the platform, but will
be the number of bytes in an object of type int, unless all structs are
aligned to a wider module on that platform, in which case it will be
whatever that module size is.
Bit-field members of unions aren't very useful.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Hans-Bernhard Bröker Guest
|
Posted: Tue Mar 06, 2007 2:43 am Post subject: Re: What's the output |
|
|
rajpal_jatin (AT) yahoo (DOT) co.in wrote:
[...]
| Quote: | union jatin rajpal;
k= sizeof(rajpal);
printf("%d",k);
|
This will print an integer number larger than 0. A result of 1 is
somewhat more likely than others, but by no means guaranteed.
| Quote: | & what would be the output if instead of union in the above example if
i'll use struct?
|
You'll get a number larger than 1.
| Quote: | Could anyon can explain me union behavior.
|
Your textbook should be able to do that. If it can't, you need a better
one.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Sat Mar 10, 2007 6:39 pm Post subject: Re: What's the output |
|
|
"Douglas A. Gwyn" <DAGwyn (AT) null (DOT) net> writes:
| Quote: | rajpal_jatin (AT) yahoo (DOT) co.in> wrote...
int main()
{
int k;
union jatin{
int i :5;
char j :2;
};
union jatin rajpal;
k= sizeof(rajpal);
printf("%d",k);
return 0;
}
[snip] |
| Quote: | As to what your program prints, it depends on the platform, but will
be the number of bytes in an object of type int, unless all structs are
aligned to a wider module on that platform, in which case it will be
whatever that module size is.
|
The program prints the size of a "union jatin" object, whose members
are a 5-bit bit field and a 2-bit bit field. Why must that union be
as big as an int? Why can't it, for example, be allocated as a single
8-bit byte?
I know that some people have claimed, and some compilers require, that
if a structure contains an int bit field, the structure must be at
least as big as an int, even if the bit field is only a few bits, but
I see no justification for this assumption in the standard (and it
doesn't make any sense to me).
(And bit fields of type char are not supported by the standard, but
may be provided as an extension.)
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|