Re: What's the output
- From: Chris Dollin <chris.dollin@xxxxxx>
- Date: Thu, 01 Mar 2007 13:40:08 +0000
jatin wrote:
On Mar 1, 6:52 am, "user923005" <dcor...@xxxxxxxxx> wrote:
On Feb 28, 5:34 pm, rajpal_ja...@xxxxxxxxxxx wrote:
int main()
{
int k;
union jatin{
int i :5;
char j :2;
};
union jatin rajpal;
k= sizeof(rajpal);
printf("%d",k);
return 0;
}
It will be the size of the largest object in the collection.
& what would be the output if instead of union in the above example if
i'll use struct?
It will be the size of the sum of all struct members, plus padding.
Could anyon can explain me union behavior.
A good C book should do nicely. Have you read K&R2?
Hi Ian and user923005
Thanks for your response.
If my understanding is correct:
When I'll use struct I'll get 8 coz
"because". "coz" is short for "cousin".
in structure I'm declaring the int
and char in bitwise manner int will store 5 bit and char will store 2
bit i.e. total 7 bit.
Plus whatever padding the compiler feels is appropriate.
when i'll printf the size of this struct then atleast i'll get output
of 1word
One /byte/. C sizes are counted in abstract units called "bytes"
or "chars", which have at least 8 -- but maybe more -- bits.
i.e. 8bit. this behaviour is ok to me.
However when i'll use union the output is 4.which is not clear to
me.
Four bytes. Your compiler -- and it will not be alone -- appears
to round union sizes to 4-bytes. It's probably a natural size on
your machine.
It should be the size of 1word. ie it should also give us the
output 8.
No "should" about it.
--
Chris "electric hedgehog" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/
.
- Follow-Ups:
- Re: What's the output
- From: jatin
- Re: What's the output
- References:
- What's the output
- From: rajpal_jatin
- Re: What's the output
- From: user923005
- Re: What's the output
- From: jatin
- What's the output
- Prev by Date: Re: What's the output
- Next by Date: Re: When to use automatic variables and when to use malloc
- Previous by thread: Re: What's the output
- Next by thread: Re: What's the output
- Index(es):
Relevant Pages
|