By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,603 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,603 IT Pros & Developers. It's quick & easy.

Doubt in structure bitfields

P: 7
Expand|Select|Wrap|Line Numbers
  1. struct date{
  2.          unsigned short int day:5;
  3.          unsigned short int mon:4;
  4.          unsigned short int year:12;
  5. }dt;
  6. dt.day=12;
  7. dt.mon=5;
  8. dt.year=2000;
  9. printf("%d:%d:%d",dt.day,dt.mon,dt.year);
In the above code the members of the structure are bitfields. we are trying to print those values using %d. "%d" is actually used to retrieve an integer of 2 or 4bytes accorind to the wordsize of the CPU, but here it is of 5,4 or 12 'BITS' only how it is retrieving correctly.
Please clarify my doubt.
Nov 9 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
Any integer type <= to the size of an int is passed to a function that does not specify the parameter type (which printf doesn't because it has a variable parameter list) as an int

This is why %d works for char, short and int as well as bit fields, they are all passed on the stack as int.
Nov 9 '06 #2

P: 7
Thank you verymuch.
Nov 12 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.