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

Variable declaration followed by colon?

P: n/a
What does this represent?

unsigned int n : 6;

???

Saverio
Jun 27 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.