Re: C parse error, why?

Nengming Zhang wrote:

> I don't do  how ":" works In C program like the following example:
> int y:1;                          /*  parse error*/
> struct _Test
> {
>        int x :1;                /* no parse error report*/
> }

This is a bit off-topic, but the answer is that C only defines
the ':' operation for fields within a struct. The idea dates 
back to when people used to worry about memory use. If you have
a struct with lots of fields that only need a few bits each, you
can save space by using the ':' option, the compiler will pack the
bits into a single word. It doesn't make much sense for "normal"
variables, the argument being that the overheads of extracting
the single bit from the word would use more space than you'd save.
As a result, K&R made it illegal, and the ANSI standard kept
this feature.


| Bill Purvis, Musical Engineer                 |
| 35 Chapel Road, Penketh, Warrington, Cheshire |
| WA5 2NG. phone: 01 925 725731                 |
| e-mail:                    |

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]