473,396 Members | 1,996 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.

Variable declaration followed by colon?

What does this represent?

unsigned int n : 6;

???

Saverio
Jun 27 '08 #1
12 17317
sa**********@gmail.com said:
What does this represent?

unsigned int n : 6;
See "The C Programming Language", 2nd edition, by Kernighan and Ritchie,
pages 149-150.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
What does this represent?
unsigned int n : 6;
Inside a structure or union, it represents an unsigned integral
bitfield that occupies 6 bits;
Outside a structure or union, it represents a syntax error.
Have fun.

Jun 27 '08 #3
On 12 Apr 2008 at 15:17, Yunzhong wrote:
What does this represent?
unsigned int n : 6;

Inside a structure or union, it represents an unsigned integral
bitfield that occupies 6 bits;
Perhaps the OP will wonder why such a thing could be useful.

A couple of common examples where bitfields are used are device drivers,
because combinations of flags (i.e. bits) are often stored in a single
byte in hardware; and binary data formats, which frequently contain
fields that aren't aligned at byte boundaries. Whether you realize it or
not, you're relying on bitfields every time you send an IP packet!
Here's the IP header struct that's used in every packet:

struct iphdr {
unsigned int version:4;
unsigned int ihl:4;
u_int8_t tos;
u_int16_t tot_len;
u_int16_t id;
u_int16_t frag_off;
u_int8_t ttl;
u_int8_t protocol;
u_int16_t check;
u_int32_t saddr;
u_int32_t daddr;
};

Jun 27 '08 #4
On Apr 12, 7:36*am, saverio.p...@gmail.com wrote:
What does this represent?

* * * * unsigned int n * : 6;
Your pitiful inability to consult a reference manual.
Jun 27 '08 #5
On 15 Apr 2008 at 2:04, ro***********@yahoo.com wrote:
On Apr 14, 5:20Â*pm, ymunt...@gmail.com wrote:
>Almost assuredly, OP is not using a fancy apple or sun machine, true.
(That code most certainly needs gcc or gcc-compatible compiler,
but that's something almost assuredly used by OP, indirectly)

The structure as presented would have *not* been correct on almost any
x86 implementation (actually on none that I know of, and I frankly
doubt there are any where it would have been).
Of course it's quite likely that the OP is using an x86, but it's not
the absolute certainty you're implying. I think the latest stats are
that 20% of laptop sales are Apples, and I imagine that C programmers
are disproportionately likely to be using interesting big-endian
hardware for their work...

Jun 27 '08 #6
On Apr 15, 2:49*am, Antoninus Twink <nos...@nospam.invalidwrote:
On 15 Apr 2008 at *2:04, robertwess...@yahoo.com wrote:
On Apr 14, 5:20*pm, ymunt...@gmail.com wrote:
Almost assuredly, OP is not using a fancy apple or sun machine, true.
(That code most certainly needs gcc or gcc-compatible compiler,
but that's something almost assuredly used by OP, indirectly)
The structure as presented would have *not* been correct on almost any
x86 implementation (actually on none that I know of, and I frankly
doubt there are any where it would have been).

Of course it's quite likely that the OP is using an x86, but it's not
the absolute certainty you're implying. I think the latest stats are
that 20% of laptop sales are Apples,

All of which are now x86. (Actually it's probable that Apple is still
moving a handful of PPC based systems to people who care more about
compatibility than price or performance).

and I imagine that C programmers
are disproportionately likely to be using interesting big-endian
hardware for their work...

All of which are now x86. (Actually it's probable that Apple is still
moving a handful of PPC based systems to people who care more about
compatibility than price or performance).

True, but mostly as embedded gear. There are darn few primary
computing* systems that aren't x86 out there (and even fewer shipping
now), older PPC based Macs are probably the biggest population.
*Desktops, laptops, servers, HPC systems - IOW, non-embedded stuff
Jun 27 '08 #7
On Tue, 15 Apr 2008 00:31:21 -0700, Kaz Kylheku wrote:
On Apr 12, 7:36*am, saverio.p...@gmail.com wrote:
>What does this represent?

* * * * unsigned int n * : 6;

Your pitiful inability to consult a reference manual.
Given a "reference manual", where would you look it up?

--
ROT-13 email address to reply
Jun 27 '08 #8
Anand Hariharan <zn********************@tznvy.pbzwrites:
On Tue, 15 Apr 2008 00:31:21 -0700, Kaz Kylheku wrote:
>On Apr 12, 7:36Â*am, saverio.p...@gmail.com wrote:
>>What does this represent?

Â* Â* Â* Â* unsigned int n Â* : 6;

Your pitiful inability to consult a reference manual.

Given a "reference manual", where would you look it up?
Exactly. His reply would almost make me think he is Nick Keighley nym
shifting. But I'm sure that's where the similarities end ..

Jun 27 '08 #9
Anand Hariharan wrote:
Kaz Kylheku wrote:
>saverio.p...@gmail.com wrote:
>>What does this represent?

unsigned int n : 6;

Your pitiful inability to consult a reference manual.

Given a "reference manual", where would you look it up?
Under "Declarations".

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #10
On Thu, 17 Apr 2008 00:09:40 -0400, CBFalconer wrote:
Anand Hariharan wrote:
>Kaz Kylheku wrote:
>>saverio.p...@gmail.com wrote:

What does this represent?

unsigned int n : 6;

Your pitiful inability to consult a reference manual.

Given a "reference manual", where would you look it up?

Under "Declarations".
Looking up K&R2 index entry for "declaration", I see page references 9, 40
and 210-218.

Pages 9 and 40 do not discuss bit fields. Pages 210-218 gets into grammar,
and does not get into bit fields until mid-way of page 212. Not the
easiest of pages in this book to find the answer I sought out to find.

There are a dozen odd more entries for declaration, but nothing that says
"declaration, with colon" or something similar.

The entry for "declaration, structure" takes one to either page 128 or
212. Bit fields are covered in the same chapter as page 128, but it does
not come until page 150.

The index itself begins with entries for symbols starting from '0' to ...
'\0' (the quotes are mine), but no entry for ':'.

This isn't a critique of K&R2's index, but just to point out that it is
not obvious where to find an answer to OP's question. Considering that one
could always mouth off a rebuttal based on "reference manual" and
"consult", I suppose I am wasting my time ....

--
ROT-13 email address to reply
Jun 27 '08 #11
Anand Hariharan <zn********************@tznvy.pbzwrote:
On Tue, 15 Apr 2008 00:31:21 -0700, Kaz Kylheku wrote:
>On Apr 12, 7:36 am, saverio.p...@gmail.com wrote:
>>What does this represent?

unsigned int n : 6;

Your pitiful inability to consult a reference manual.

Given a "reference manual", where would you look it up?
": (colon punctuator)" is in the Standard's index and points to exactly
the right place.

-Larry Jones

I think your train of thought is a runaway. -- Calvin's Mom
Jun 27 '08 #12
sa**********@gmail.com wrote:
What does this represent?

unsigned int n : 6;

???
As shown this is a syntax error. Within a structure or union
declaration, this declares 'n' to be a "bit field" of 6 bits width and
of type unsigned int.

See:

<http://c-faq.com/struct/bitfield0.html>

Jun 27 '08 #13

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
14
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
15
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here...
1
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.