473,395 Members | 1,649 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,395 software developers and data experts.

Colon initialiser?

Is anyone familiar with the following syntax?

<type<id>:<expression>;

e.g.

double d:2+3;

Ive found it in a "standard C" grammar. gcc wont accept it.

Thanks.

Jan 24 '07 #1
11 1611
al*********@gmail.com wrote:
Is anyone familiar with the following syntax?

<type<id>:<expression>;

e.g.

double d:2+3;

Ive found it in a "standard C" grammar. gcc wont accept it.
I expect you'll find that piece of the grammar is there for
bitfields /inside structs/ eg:

struct example
{
unsigned c: 1;
unsigned v: 1;
unsigned z: 1;
unsigned p: 1;
};

The bitfield rule of thumb is: don't use them, unless you /know/
you need to use them.

--
Chris "electric hedgehog" Dollin
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Jan 24 '07 #2


On Jan 24, 2:48 pm, Chris Dollin <chris.dol...@hp.comwrote:
alan.lar...@gmail.com wrote:
Is anyone familiar with the following syntax?
<type<id>:<expression>;
e.g.
double d:2+3;
Ive found it in a "standard C" grammar. gcc wont accept it.I expect you'll find that piece of the grammar is there for
bitfields /inside structs/ eg:

struct example
{
unsigned c: 1;
unsigned v: 1;
unsigned z: 1;
unsigned p: 1;
};

The bitfield rule of thumb is: don't use them, unless you /know/
you need to use them.
Ah, good thinking. Thanks.

Jan 24 '07 #3
al*********@gmail.com wrote:
Is anyone familiar with the following syntax?

<type<id>:<expression>;

e.g.

double d:2+3;

Ive found it in a "standard C" grammar. gcc wont accept it.

Thanks.
Thats to specify the width of a bitfield. Outside of a structure or
union, it's a syntax error.
Moreover, bitfields are always of type unsigned int. Particularly, they
cannot be a floating point type.

Jan 24 '07 #4
In article <11**********************@s48g2000cws.googlegroups .com>,
santosh <sa*********@gmail.comwrote:
>Moreover, bitfields are always of type unsigned int. Particularly, they
cannot be a floating point type.
"A bit-field shall have a type that is a qualified or unqualified
version of one of int, unsigned int, or signed int. Whether the
high-order bit position of a (possibly qualified) "plain" int bit-field
is treated as a sign bit is implementation-defined. A bit-field is
interpreted as an integral type consisting of the specified number
of bits."

So there you have it: bitfields can be signed int, and if the
qualifier "signed" is used explicitly, one of the bits is a sign
bit (whereas if "signed" is omitted, the field might be treated
as unsigned.)

--
Prototypes are supertypes of their clones. -- maplesoft
Jan 24 '07 #5
"santosh" <sa*********@gmail.comwrites:
Moreover, bitfields are always of type unsigned int.
No. From C99 6.7.2.1:

A bit-field shall have a type that is a qualified or
unqualified version of _Bool, signed int, unsigned int, or
some other implementation-defined type.
Particularly, they cannot be a floating point type.
This is true (for portable programs).
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
Jan 24 '07 #6


On Jan 24, 10:23 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
"santosh" <santosh....@gmail.comwrites:
Moreover, bitfields are always of type unsigned int.No. From C99 6.7.2.1:

A bit-field shall have a type that is a qualified or
unqualified version of _Bool, signed int, unsigned int, or
some other implementation-defined type.
Thanks to both you and Walter Roberson for the correction.

Incidentally, does the C standard define the size of the _Bool type,
and what would be the purpose of a bitfield of type _Bool, when _Bool
itself behaves like a single bit?

Jan 24 '07 #7
santosh wrote:
On Jan 24, 10:23 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
"santosh" <santosh....@gmail.comwrites:
Moreover, bitfields are always of type unsigned int.No. From C99 6.7.2.1:
A bit-field shall have a type that is a qualified or
unqualified version of _Bool, signed int, unsigned int, or
some other implementation-defined type.

Thanks to both you and Walter Roberson for the correction.

Incidentally, does the C standard define the size of the _Bool type,
I don't believe it does.
and what would be the purpose of a bitfield of type _Bool, when _Bool
itself behaves like a single bit?
struct { unsigned i : 1; _Bool j : 1 } s = { 2, 2 };

s.i is zero, but s.j is one.

Jan 24 '07 #8
santosh wrote:
>
On Jan 24, 10:23 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
>"santosh" <santosh....@gmail.comwrites:
>>Moreover, bitfields are always of type unsigned int.No. From C99 6.7.2.1:
A bit-field shall have a type that is a qualified or
unqualified version of _Bool, signed int, unsigned int, or
some other implementation-defined type.

Thanks to both you and Walter Roberson for the correction.

Incidentally, does the C standard define the size of the _Bool type,
No, it doesn't.
and what would be the purpose of a bitfield of type _Bool, when _Bool
itself behaves like a single bit?
It doesn't just behave like a single bit when it comes to converting
values from other types. When a value is converted to _Bool *any*
non-zero value is interpreted as 1 (i.e. true):

struct
{
unsigned u:1;
_Bool b:1;
} foo;

foo.u = 42;
foo.b = 42;

//foo.u == 0
//foo.b == 1
--
Clark S. Cox III
cl*******@gmail.com
Jan 24 '07 #9
"santosh" <sa*********@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
Incidentally, does the C standard define the size of the _Bool type,
and what would be the purpose of a bitfield of type _Bool, when _Bool
itself behaves like a single bit?
A typical implementation will give _Bool the same representation as an
(unsigned?) int, which wastes a lot of space. Packing a bunch of 1-bit
_Bools into a structure means less space used, though usually at the
expense of performance. You'd need a _lot_ of _Bools before it made
sense to to that on modern machines, though one can argue that applies
to just about any use of bitfields.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Jan 24 '07 #10
"Stephen Sprunk" <st*****@sprunk.orgwrites:
"santosh" <sa*********@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
Incidentally, does the C standard define the size of the _Bool type,
and what would be the purpose of a bitfield of type _Bool, when _Bool
itself behaves like a single bit?

A typical implementation will give _Bool the same representation as an
(unsigned?) int, which wastes a lot of space. Packing a bunch of
1-bit _Bools into a structure means less space used, though usually at
the expense of performance. You'd need a _lot_ of _Bools before it
made sense to to that on modern machines, though one can argue that
applies to just about any use of bitfields.
gcc, IBM's xlc, and Intel's icc all have sizeof(_Bool)==1. (Of
course, that's 1 byte, which happens to be 8 bits on all those
compilers.)

--
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 25 '07 #11
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Stephen Sprunk" <st*****@sprunk.orgwrites:
>A typical implementation will give _Bool the same representation as
an
(unsigned?) int, which wastes a lot of space. Packing a bunch of
1-bit _Bools into a structure means less space used, though usually
at
the expense of performance. You'd need a _lot_ of _Bools before it
made sense to to that on modern machines, though one can argue that
applies to just about any use of bitfields.

gcc, IBM's xlc, and Intel's icc all have sizeof(_Bool)==1. (Of
course, that's 1 byte, which happens to be 8 bits on all those
compilers.)
Hmm. I thought when I'd tested mine it was 4, but it's showing 1 now.
Still, a struct with 1-bit bool bitfields should still give you a
_minimum_ of a factor of eight improvement in storage space (i.e.
whatever CHAR_BIT is). And, as I said, that's unlikely to ever matter.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Jan 25 '07 #12

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

Similar topics

0
by: Cindy Mueller | last post by:
--F0.5BD08B6_ABBB95C Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%">...
0
by: Maurice Mclain | last post by:
--7FC.__338__A.F0643A1 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1"...
0
by: Lola Ervin | last post by:
--7F90A0.__A71 Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%"> <tr>
0
by: Carl Frey | last post by:
--E.3EC5.46C.DE_FF_ Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%">...
0
by: Cecile Hilton | last post by:
--E.3EC5.46C.DE_FF_ Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%">...
0
by: Desiree Cho | last post by:
--__9_.C6_2.F04.5.E.4__B4_ Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1"...
0
by: Scottie Wheeler | last post by:
--_.D97.0B.DC3_EB Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html> <body> <div align=3D"center"> <center> <table border=3D"0" cellspacing=3D"1" width=3D"100%">...
5
by: Andrew Poulos | last post by:
If I have some code in a function that looks something like this: if (x) { for (var i = 0; i < a; i++) { //blah } } else { for (var i = 0; i < b; i++) { //blah }
161
by: Dan Lenski | last post by:
Hi all, I'm a recent, belated convert from Perl. I work in a physics lab and have been using Python to automate a lot of measurement equipment lately. It works fabulously for this purpose. ...
22
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved...
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
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...
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.