473,763 Members | 3,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1789
Ico
ch*******@yahoo .com <ch*******@yaho o.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....fo r 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*******@yaho o.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....fo r 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*******@yaho o.com"
<ch*******@yaho o.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...@mindsp ring.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*******@yaho o.com" <ch*******@yaho o.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....fo r 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_Keit h) 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*******@yaho o.com"
<ch*******@yaho o.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

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

Similar topics

2
1613
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 whether the errors I'm getting is because of that, or because i have declared the union instances wrong, can you please help. struct crecord { char customercode; char customername;
3
2060
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 function displays the address of each of the 5 integers twice, first using the member pointer table "members" and then using an instance of myclass. The results are surprising: the values for x and y are incorrect when using the member pointers.
15
5283
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 thing? I guess there might be some additional overhead with CLASSES, but is that really an issue with today's computers?
1
4342
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 the following example but I would like to know whether this is a gcc extension or whether it's actually part of the C99 standard. Did previous C standards allow this? #include <stdio.h>
16
3954
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 value is declared to a variable in the union, the existing value is overwritten even though the new value is declared to a different variable than that of the first value. Now I'm just wondering what the use of this is. I'm sure there are lots,...
9
3094
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 numeric data types). Do such programmers also have conventions for naming structures and unions (e.g. do they like their names to begin with s and u, so that, when the . operator is used, it is clear whether a structure or union is being accessed...
23
2831
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 relevance.Though both of these(bitfields and unions) are used where space is a constraint(so I can assume always in embedded systems,where memory is particularly less)and we want to save space/memory. As far as I have read, there is no...
10
1818
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 Double;
67
3359
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 sake of Unions simply because I wanted to see one in action. Granted: it makes it possible to save a few bytes of storage when you have something that can be a chicken or a rooster, but not both, and you're always going to know which it is. ...
17
3027
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 want to do it with code that does NOT use shifts (<<) , bit- operations (| &) !!
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.