1

I compiled this (gcc compiler):

#include <stdio.h>

main() {

    struct {
        a:1;
        b:2;
    } t;

    t.b=6;
    t.a=2;
    printf("%d %d",t.a,t.b);
}

Error shown was:

expected specifier-qualifier list before 'a'

Why it is needed when unnamed data variables are taken as int by default. Please explain...

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
Abhay
  • 129
  • 2
  • 11
  • "unnamed data variables are taken as int by default": looks like you have a very old version of C in mind. If you compile your program with `-Wall` you should also receive a warning for the "unconventional" declaration of `main`. – Jens Gustedt Apr 07 '12 at 19:46
  • More comments on your code: `main()` should be `int main(void)`; bit fields should usually be declared as `unsigned`; the values you're assigning to `t.b` and `t.a` are outside the range of values they can necessarily hold. – Keith Thompson Apr 07 '12 at 21:20

1 Answers1

1

This is required by the C language standard. According to section 6.7.2.1 part 4:

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523