unions & structs...

C

chump1708

union u
{
struct st
{
int i : 4;
int j : 4;
int k : 4;
int l; // or int l:4
}st;
int i;
}u;
main()
{
u.i = 100;
printf("%d, %d, %d",u.i, u.st.i, u.st.l);
}

ok...the answer is 100,4,0...
can anyoone explain the program and bit fields??
 
I

Ico

union u
{
struct st
{
int i : 4;
int j : 4;
int k : 4;
int l; // or int l:4
}st;
int i;
}u;
main()
{
u.i = 100;
printf("%d, %d, %d",u.i, u.st.i, u.st.l);
}

ok...the answer is 100,4,0...
can anyoone explain the program and bit fields??

Chump,

What is it you are trying to do ? You keep posting *wrong* pieces of
code, not compiling or causing undefined behaviour, and you expect
people to explain you what is happening. Please get yourself a proper
textbook about C and find out for yourself why you get those results.

plonk
 
C

chump1708

I dont think the above code is wrong...Its correct...all I am trying to
do is learn...n even if I read hundreds of books, I won't be able to
answer such questions....for that matter...no one...it all comes by
experience...if u don't know the answer, learn like me...there are
experts here who can answer the questions excellently...I am trying to
learn the way they think....Just try to think their way...n u will be
expert someday....
 
M

Marc Boyer

Le 17-01-2006 said:
I dont think the above code is wrong...Its correct...all I am trying to
do is learn...n even if I read hundreds of books,

There a only three books to read:
- K&R
- "C A reference manual", by Harbison and Stelle
- The C standart
I won't be able to answer such questions....

No, read the 2 first, and you will be able to.
for that matter...no one...it all comes by experience...

No.
there are
experts here who can answer the questions excellently...

Yes. But did you think they want to answer ?
I am trying to learn the way they think....

Read good books.

Marc Boyer
 
E

Eric Sosman

I dont think the above code is wrong...Its correct...all I am trying to
do is learn...n even if I read hundreds of books, I won't be able to
answer such questions....for that matter...no one...it all comes by
experience...if u don't know the answer, learn like me...there are
experts here who can answer the questions excellently...I am trying to
learn the way they think....Just try to think their way...n u will be
expert someday....

Something about this thread recalls an old "Calvin
and Hobbes" cartoon. Paraphrased:

CALVIN: Mom, do we have any chainsaws in the house?

HIS MOTHER: No.

CALVIN: Phooey! How am I going to learn to juggle?
 
P

pete

M

Mark McIntyre

I dont think the above code is wrong..

There /is/ no above code, since you didn't bother to include any
context in your reply.
.Its correct...
no

all I am trying to do is learn...

Thats fine, but please, do buy a good book and read it first. You
wouldn't learn aboit poisons or building nuclear bombs by trial and
error.
Just try to think their way...n u will be expert someday....

unfortunately Grasshopper, computer programming isn't actually a zen
experience.
:)

Mark McIntyre
 
T

thomas.mertes

K

Keith Thompson

I dont think the above code is wrong...Its correct...all I am trying to
do is learn...n even if I read hundreds of books, I won't be able to
answer such questions....for that matter...no one...it all comes by
experience...if u don't know the answer, learn like me...there are
experts here who can answer the questions excellently...I am trying to
learn the way they think....Just try to think their way...n u will be
expert someday....

Some advice.

Please don't use silly abbreviations like "u" for "you" and "n" for
"and". They just make it more difficult for us to read what you
write. Standard punctuation is also helpful. This is a newsgroup,
not a chat room.

If you want to learn C, start with the basics. Get a copy of K&R2
(Kernighan & Ritchie, _The C Programming Language_, 2nd Edition) and
work through it.

Finally, please provide some context when you post a followup. Google
groups makes this gratuitously difficult, but there are workarounds;
see <http://cfaj.freeshell.org/google/> for details. (Most of us
can't easily see the article to which you're replying.)
 
M

Mark McIntyre


int main(void)
{
u.i = 100;
printf("%d, %d, %d",u.i, u.st.i, u.st.l);

warning C4013: 'printf' undefined; assuming extern returning int

you MUST include stdio.h when using printf. If you don't, then your
code has a massive bug.
ok...the answer is 100,4,0...

How members of a struct and union are stored is implementation
specific. There may be padding between them, and it may not be
possible to access the union as adifferent type to the type you used
to store into it.
I suspect that if you look at the bits in i, and map them onto the
bits in st. you will see how it arrives at the answer. Howevr this is
not a certainty.
can anyoone explain the program and bit fields??

Whoa there. You confess to knowing very little about C, and already
you want to learn bitfields. In 20 years of programming, I've very
rarely needed them, though others undoubtedly will differ in
experience. Nevertheless, step back and understand how to write simple
programmes first, like the examples in K&R2.
Mark McIntyre
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
union u
{
struct st
{
int i : 4;
int j : 4;
int k : 4;
int l; // or int l:4
}st;
int i;
}u;
main()
{
u.i = 100;
printf("%d, %d, %d",u.i, u.st.i, u.st.l);
}

ok...the answer is 100,4,0...

The result is not defined by the standard. It may be defined by your
implementation, but it's not portable. This is not a proper way of using
the bitfields. Don't do that.
can anyoone explain the program and bit fields??

Bitfields ar just 'small variables' useful to save memory space (with a
decrease of performance due to code overhead: massive use of bitwise
operators).
 
E

Emmanuel Delahaye

Ico a écrit :
What is it you are trying to do ? You keep posting *wrong* pieces of
code, not compiling or causing undefined behaviour, and you expect
people to explain you what is happening. Please get yourself a proper
textbook about C and find out for yourself why you get those results.

plonk

You have forgotten to take your pills...

plonk yourself...
 
I

Ico

Emmanuel Delahaye said:
Ico a écrit :

You have forgotten to take your pills...

You are right, how thoughtless of me ! I'll get myself a handfull of
Doctor Prlwytzkofsky's magic bright pink pills right away, which will
deprive me of the irresistible need of criticizing people who are
flooding the newsgroup with broken code and meaningless questions. That
should fix me up for the next few hours. Thank you for pointing that
out.
plonk yourself...

I just tried, but my newsreader wouldn't let me, sorry.

Ico
 
C

Chuck F.

Ico said:
What is it you are trying to do ? You keep posting *wrong*
pieces of code, not compiling or causing undefined behaviour,
and you expect people to explain you what is happening. Please
get yourself a proper textbook about C and find out for yourself
why you get those results.

plonk

Asking ignorant questions is no reason to plonk someone.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
C

Chuck F.

I dont think the above code is wrong...Its correct...all I am
trying to do is learn...n even if I read hundreds of books, I
won't be able to answer such questions....for that matter...no
one...it all comes by experience...if u don't know the answer,
learn like me...there are experts here who can answer the
questions excellently...I am trying to learn the way they
think....Just try to think their way...n u will be expert
someday....

What above code? You failed to include any context. Also please
use proper language and punctuation. u and n and ... only make
reading hard. If you want to be read by others, make it easy. And
make sure to read the URL reference below before posting again.

Usenet is NOT google groups. Usenet has been around for decades,
and has been working well. The google interface only encourages
the ignorant to foul the system.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
C

chump1708

Hello everyone,
Why don't you people reply to questions asked, instead of writing
such weird statements.
Thanks,
C_Learner.
 
I

Ingo Menger

Hello everyone,
Why don't you people reply to questions asked, instead of writing
such weird statements.

Ok, I'll try my best.

What did you expect?

Certainly.
Since you wrote the program, maybe you explain it to us?
Regarding bitfields, they are already explained in relevant books. Just
read them.

And, by the way:

# include <stdio.h>
int main(void) { int i; printf("%d\n", i); return 0; }

The output is 3245786.
Can you explain the program and integer variables?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top