473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

unions & structs...

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??

Jan 17 '06 #1
18 1753
Ico
ch*******@yahoo.com <ch*******@yahoo.com> wrote:
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

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 17 '06 #2
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....

Jan 17 '06 #3
Le 17-01-2006, ch*******@yahoo.com <ch*******@yahoo.com> a écrit*:
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
Jan 17 '06 #4


ch*******@yahoo.com wrote On 01/17/06 11:10,:
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?

--
Er*********@sun.com

Jan 17 '06 #5
ch*******@yahoo.com wrote:

I dont think the above code is wrong...Its correct...
It's wrong.
There is no version of C that allows a form of main
which has neither a return type nor a return statement.
all I am trying to do is learn...


Read this:
http://www.ungerhu.com/jxh/clc.welcome.txt

Read this:
http://www.eskimo.com/~scs/C-faq/top.html

And download a copy of N869, until you can afford something better:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/

--
pete
Jan 17 '06 #6
On 17 Jan 2006 08:10:19 -0800, in comp.lang.c , "ch*******@yahoo.com"
<ch*******@yahoo.com> wrote:
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
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 17 '06 #7
pete <pfil...@mindspring.com> wrote:
There is no version of C that allows a form of main
which has neither a return type nor a return statement.


Well the "hello world" example from K & R is:

#include <stdio.h>
main()
{
printf("hello, world\n");
}

This main has no return type (default is int) and no
return statement.

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Wikipedia: http://en.wikipedia.org/wiki/Seed7
Project page: http://sourceforge.net/projects/seed7

Jan 17 '06 #8
"ch*******@yahoo.com" <ch*******@yahoo.com> writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.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.
Jan 17 '06 #9
On 17 Jan 2006 07:52:52 -0800, in comp.lang.c , "ch*******@yahoo.com"
<ch*******@yahoo.com> wrote:
main()
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
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 17 '06 #10
ch*******@yahoo.com 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).

--
A+

Emmanuel Delahaye
Jan 17 '06 #11
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...

--
A+

Emmanuel Delahaye
Jan 17 '06 #12
th***********@gmx.at a écrit :
pete <pfil...@mindspring.com> wrote:
There is no version of C that allows a form of main
which has neither a return type nor a return statement.


Well the "hello world" example from K & R is:

#include <stdio.h>
main()
{
printf("hello, world\n");
}

This main has no return type (default is int) and no
return statement.


So what ? No book is error-free. See the errata :

http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html

--
A+

Emmanuel Delahaye
Jan 17 '06 #13
Ico
Emmanuel Delahaye <em***@yourbranoos.fr> wrote:
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...


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

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 17 '06 #14
Ico wrote:
ch*******@yahoo.com <ch*******@yahoo.com> wrote:
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??


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/>
Jan 17 '06 #15
ch*******@yahoo.com wrote:

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/>
Jan 17 '06 #16
Ico
Chuck F. <cb********@yahoo.com> wrote:
Ico wrote:

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.


You're right, I just had a bad day yesterday...

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 18 '06 #17
Hello everyone,
Why don't you people reply to questions asked, instead of writing
such weird statements.
Thanks,
C_Learner.

Ico wrote:
Chuck F. <cb********@yahoo.com> wrote:
Ico wrote:

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.


You're right, I just had a bad day yesterday...

--
:wq
^X^Cy^K^X^C^C^C^C


Jan 18 '06 #18

ch*******@yahoo.com schrieb:
Hello everyone,
Why don't you people reply to questions asked, instead of writing
such weird statements.


Ok, I'll try my best.
ok...the answer is 100,4,0...
What did you expect?
can anyoone explain the program and bit fields??


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?

Jan 18 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: muser | last post by:
sorry to post so soon after having posted before. I'm using a union and I've read that a union can only hold a value of one member at a time. The following function uses a union, but i don't know...
3
by: Udo Steinberg | last post by:
Hi, The program below stores the 5 unsigned ints a, b, c, x and y in an anonymous union, so that they share storage space with state. Additionally x and y share storage space with nums. The main...
15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
1
by: Neil Zanella | last post by:
Hello, In C++ AFAIK structure assignment copies every data member element by element. This rule also works with the gcc 3.2.2 C compiler and even works when one of the members is a union as in...
16
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
9
by: Neil Zanella | last post by:
Hello, Some programmers like to use a coding convention where all names of variables that are pointers start with the letter p (and sometimes even use similar conventions for strings and other...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
10
by: Koen | last post by:
Hi! Does anyone know what the standard says about the way unions are stored in C? I mean the following: Let's say you have a union with a double and a char field: union MyUnion_t { double...
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
17
by: anon.asdf | last post by:
Hi! I want to assign the number 129 (binary 10000001) to the MSB (most significant byte) of a 4-byte long and leave the other lower bytes in- tact! -working on normal pentium (...endian) I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.