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 6564243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:56:32+00:00 2026-05-25T13:56:32+00:00

Someone drew my attention to the following program: #include <stdio.h> struct X50 { long

  • 0

Someone drew my attention to the following program:

#include <stdio.h>

struct X50 {
 long long int z:50;
} s50 = { 2 };

struct X10 {
 long long int z:10;
} s10 = { 2 };

int main() {
  printf("%zu %zu %zu\n",sizeof(long long int),sizeof(s10.z+1),sizeof(s50.z+1));
}

The type of expression sizeof(lv.z+1) is computed according to the “usual arithmetic conversions”, that pretty much say that the size of the type for the lvalue lv.z will be reflected on the type of the addition, as long as it is at least int.

I did not expect this type to depend on the size of the bitfield, but it does: both GCC and Clang print 8 4 8 on my computer.

The relevant clauses that I found in the C99 standard are clause 2 in 6.3.1.1, that does not seem so say anything about bitfields not based on _Bool, int, signed int, or unsigned int. The second part of the clause, “If an int can represent all values of the original type, the value is converted to an int, …”, only seems to apply in the conditions described in the first part of the clause, that do not include bitfields based on long long int.

Besides, 6.7.2.1 says:

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.

Is it the case that since long long int bitfields are outside the scope of the standard, compilers can invent their own rules, or can some kind of justification for Clang and GCC’s behaviors be found elsewhere in C99?

I found this question on StackOverflow, which points in the “compilers can invent their own rules” direction, but there could still be a justification that I missed for Clang and GCC both typing S10.z as int.

  • 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-05-25T13:56:32+00:00Added an answer on May 25, 2026 at 1:56 pm

    6.7.2.1 paragraph 10 (emphasis added):

    An implementation may allocate any addressable storage unit large enough to hold a bit-
    field.
    If enough space remains, a bit-field that immediately follows another bit-field in a
    structure shall be packed into adjacent bits of the same unit. If insufficient space remains,
    whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is
    implementation-defined. The order of allocation of bit-fields within a unit (high-order to
    low-order or low-order to high-order) is implementation-defined. The alignment of the
    addressable storage unit is unspecified.

    So in answer to your question (which I no longer think is a duplicate), if a compiler allows an implementation-defined type to be used as the type of a bitfield, it doesn’t appear to be required to allocate enough size for that type, only enough size for the actual bitfield. Of course, it also appears to be within its rights to allocate 4 kilobytes for the bitfield.

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

Sidebar

Related Questions

Someone just sent me a decompile of a program into C. It was a
Someone asked a similar question before , getting the following error when I run
Someone brought this article to my attention that claims (I'm paraphrasing) the STL term
Someone please explain what is the main idea of using strings.xml ? I think
Someone told me about a C++ style difference in their team. I have my
Someone please correct me if I'm wrong, but parsing a yyyy/MM/dd (or other specific
Someone asked me how familiar I am with VC++ and how familiar I am
Someone told me about swamp diagrams explaning that they were useful to predict code
Someone told me that it's faster to concatenate strings with StringBuilder. I have changed
Someone asked me a question via e-mail about integer partitions the other day (as

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.