Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #63533

Re: What is the output of this program ?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: What is the output of this program ?
Date 2015-06-18 10:14 -0700
Organization None to speak of
Message-ID <ln7fr1j5vr.fsf@nuthaus.mib.org> (permalink)
References <b3e47c82-8ba3-4d5d-bf40-6f87a390b15a@googlegroups.com> <5582F12D.3070708@verizon.net>

Show all headers | View raw


James Kuyper <jameskuyper@verizon.net> writes:
> On 06/18/2015 09:45 AM, Rahul Kumar wrote:
>> #include <stdio.h>
>> 
>> struct s
>> {
>> 	int a:4;
>> 	char b:5;
>> 	int c:7;
>> 	unsigned f1:1;
>> 	unsigned f2:24;
>> 	unsigned :3;
>> 	int :3;// anonymous b.f. -- not accessible
>> 	int :0;	// significance -- align to next word boundary -- 47 bit < 64 bit -- align to 64 bit(8byte)
>> 	unsigned f3:6;	// 70 bit
>> 	unsigned :0; 	// ????? -- 96
>> 	unsigned f4:8;	//104 -- 13 byte -- 16 byte size
>> }x;
>> 
>> int main()
>> {
>> 	printf("%d\n",sizeof(x));
>> }
>> 
>> how to calculate output of this program,please help me,i am just confused with it.
>
> My first answer was correct, but not particularly detailed. I'll give
> you some more details this time.
>
> As Richard has already mentioned, a compiler is not required to accept a
> bit-field of type 'char', so this code can only be ported to compilers
> that accept that type.
> A bit-field width is not allowed to exceed the size in bits of the
> corresponding type. Therefore, the declaration of f2 is allowed only on
> implementations where "unsigned int" has at least 24 bits.

Another complication: the type of a bit field can the layout of the
containing structure, even for bit fields of the same size.  Some
ABIs require this, though there's no hint of it in the C standard.

For example, on my system this program:

    #include <stdio.h>
    int main(void) {
        struct s1 { unsigned char      bf:1; };
        struct s2 { unsigned short     bf:1; };
        struct s3 { unsigned int       bf:1; };
        struct s4 { unsigned long      bf:1; };
        struct s5 { unsigned long long bf:1; };
        printf("%zu %zu %zu %zu %zu\n",
               sizeof (struct s1),
               sizeof (struct s2),
               sizeof (struct s3),
               sizeof (struct s4),
               sizeof (struct s5));
    }

produces this output:

    1 2 4 8 8

Of course the output, and even the legality of the program itself, are
implementation-defined, and the actual output is within the bounds of
what the standard permits.

> It is implementation-defined whether a bit-field of type 'int' has a
> signed or unsigned type. Since it's almost never a good idea to be
> unsure whether a type is signed or unsigned, I recommend replacing "int"
> with "signed int" (or, more simply, "signed") in all of the above member
> declarations. The same is probably true for any implementation that
> allows bit-fields of "char" type. I recommend explicitly declaring "b"
> as either "signed char" or "unsigned char", depending upon what you
> actually want it to be.

It probably doesn't matter whether an anonymous bit field is signed
or unsigned, but yes, specifying `unsigned int` or `signed int`
rather than just `int` is a good habit.  Most bit fields should
probably be unsigned, but it depends on what they're used for.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

What is the output of this program ? Rahul Kumar <rahulkumar5july@gmail.com> - 2015-06-18 06:45 -0700
  Re: What is the output of this program ? James Kuyper <jameskuyper@verizon.net> - 2015-06-18 10:10 -0400
  Re: What is the output of this program ? Richard Heathfield <rjh@cpax.org.uk> - 2015-06-18 15:22 +0100
    Re: What is the output of this program ? Keith Thompson <kst-u@mib.org> - 2015-06-18 08:07 -0700
      Re: What is the output of this program ? Richard Heathfield <rjh@cpax.org.uk> - 2015-06-18 16:11 +0100
    Re: What is the output of this program ? Ralf Damaschke <rwspam@gmx.de> - 2015-06-18 18:48 +0000
      Re: What is the output of this program ? Richard Heathfield <rjh@cpax.org.uk> - 2015-06-18 23:01 +0100
    Re: What is the output of this program ? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 08:31 -0700
      Re: What is the output of this program ? jadill33@gmail.com - 2015-07-13 07:51 -0700
        Re: What is the output of this program ? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-15 02:51 -0700
          Re: What is the output of this program ? jadill33@gmail.com - 2015-07-15 05:55 -0700
            Re: What is the output of this program ? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-20 10:01 -0700
      Re: What is the output of this program ? Richard Heathfield <rjh@cpax.org.uk> - 2015-07-13 22:07 +0100
        Re: What is the output of this program ? Keith Thompson <kst-u@mib.org> - 2015-07-13 14:16 -0700
        Re: What is the output of this program ? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-15 02:11 -0700
          Re: What is the output of this program ? Richard Heathfield <rjh@cpax.org.uk> - 2015-07-15 10:58 +0100
            Re: What is the output of this program ? Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-20 10:05 -0700
  Re: What is the output of this program ? James Kuyper <jameskuyper@verizon.net> - 2015-06-18 12:26 -0400
    Re: What is the output of this program ? Keith Thompson <kst-u@mib.org> - 2015-06-18 10:14 -0700

csiph-web