Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:03:23+00:00 2026-06-16T04:03:23+00:00

I wonder why bitfields work with unions/structs but not with a normal variable like

  • 0

I wonder why bitfields work with unions/structs but not with a normal variable like int or short.
This works:

struct foo {
    int bar : 10;
};

But this fails:

int bar : 10; // "Expected ';' at end of declaration"

Why is this feature only available in unions/structs and not with variables? Isn’t it technical the same?


Edit:

If it would be allowed you could make a variable with 3 bytes for instance without using the struct/union member each time. This is how I would to it with a struct:

struct int24_t {
    int x : 24 __attribute__((packed));
};

struct int24_t var; // sizeof(var) is now 3
// access the value would be easier:
var.x = 123;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:03:25+00:00Added an answer on June 16, 2026 at 4:03 am

    This is a subjective question, “Why does the spec say this?” But I’ll give it my shot.

    Variables in a function normally have “automatic” storage, as opposed to one of the other durations (static duration, thread duration, and allocated duration).

    In a struct, you are explicitly defining the memory layout of some object. But in a function, the compiler automatically allocates storage in some unspecified manner to your variables. Here’s a question: how many bytes does x take up on the stack?

    // sizeof(unsigned) == 4
    unsigned x;
    

    It could take up 4 bytes, or it could take up 8, or 12, or 0, or it could get placed in three different registers at the same time, or the stack and a register, or it could get four places on the stack.

    The point is that the compiler is doing the allocation for you. Since you are not doing the layout of the stack, you should not specify the bit widths.

    Extended discussion: Bitfields are actually a bit special. The spec states that adjacent bitfields get packed into the same storage unit. Bitfields are not actually objects.

    1. You cannot sizeof() a bit field.

    2. You cannot malloc() a bit field.

    3. You cannot &addressof a bit field.

    All of these things you can do with objects in C, but not with bitfields. Bitfields are a special thing made just for structures and nowhere else.

    About int24_t (updated): It works on some architectures, but not others. It is not even slightly portable.

    typedef struct {
        int x : 24 __attribute__((packed));
    } int24_t;
    

    On Linux ELF/x64, OS X/x86, OS X/x64, sizeof(int24_t) == 3. But on OS X/PowerPC, sizeof(int24_t) == 4.

    Note the code GCC generates for loading int24_t is basically equivalent to this:

    int result = (((char *) ptr)[0] << 16) |
                 (((unsigned char *) ptr)[1] << 8) |
                 ((unsigned char *)ptr)[2];
    

    It’s 9 instructions on x64, just to load a single value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder why the Collection.addAll() method only accepts other Collection s but not Iterable
I wonder, does there exist something like Velocity , but for internal use? Essentially
I wonder if I can create a RelayCommand on my ViewModel like this: public
wonder if anyone can help - I thought I had solved this but i
I wonder how to create Zune-like apps (I mean Metro style). But I don't
wonder whether someone can help me with the following one... I have a struct
I wonder, using Eclipse's PyDev plugin , how come documentation does not always show
I wonder whether someone has an idea for how to count combinations like the
Wonder if anyone can give me a straight answer for this. It's mostly curiosity,
Wonder if this possible. Saw many posts on adding watermark after the pdf is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.