title: Is there a bit-equivalent of sizeof() in C?
body: <p>Sizeof() doesn't work when applied to bitfields:</p>
  
  <pre><code># cat p.c
    #include&lt;stdio.h&gt;
    int main( int argc, char **argv )
    {
      struct { unsigned int bitfield : 3; } s;
      fprintf( stdout, "size=%d\n", sizeof(s.bitfield) );
    }
  # gcc p.c -o p
    p.c: In function ‘main’:
    p.c:5: error: ‘sizeof’ applied to a bit-field
  </code></pre>
  
  <p>...obviously, since it can't return a floating point partial size or something. However, it brought up an interesting question. <strong>Is</strong> there an equivalent, in C, that will tell you the number of bits in a variable/type? Ideally, it would also work for regular types as well, like <em>char</em> and <em>int</em>, in addition to bitfields.</p>
  
  <blockquote>
    <p>Update:</p>
    
    <p>If there's no language equivalent of sizeof() for bitfields, what is the most efficient way of calculating it - at runtime! Imagine you have loops that depend on this, and you don't want them to break if you change the size of the bitfield - and no fair cheating and making the bitfield size and the loop length a macro. ;-)</p>
  </blockquote>