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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:03:29+00:00 2026-06-17T07:03:29+00:00

I am trying to access bit fields: Below is the code which works fine

  • 0

I am trying to access bit fields:

Below is the code which works fine and gives expected result but throws compiler warnings which I have mentioned below.

#include <stdio.h>
#include <stdint.h>


struct status_type_one{
unsigned delta_cts : 1;// lsb
unsigned delta_dsr : 1;
unsigned tr_edge : 1 ;
unsigned delta_rec : 1;
unsigned cts : 1;
unsigned dsr : 1;
unsigned ring : 1;
unsigned rec_line : 1;// msb
} status_one;

struct status_type_two {
unsigned : 4; // lsb 4 bits
unsigned cts : 1; //bit 5
unsigned dsr : 1; // bit 6
} status_two;



int main(void)
{
  status_one.delta_cts=1;
  status_one.delta_dsr=0;
  status_one.tr_edge=1;
  status_one.delta_rec=0;
  status_one.cts=1;
  status_one.dsr=0;
  status_one.ring=1;
  status_one.rec_line=1;

  printf("The value of status_one is %x\n",status_one);  // warning here

  status_two.cts=1;
  status_two.dsr=1;

  printf("The value of status_one is %d\n",status_two);  // warning here



    return 0;
    }

But I am getting below warning:

$ gcc -Wall Bit_Fields.c -o Bit_Fields
Bit_Fields.c: In function `main':
Bit_Fields.c:35: warning: unsigned int format, status_type_one arg (arg 2)
Bit_Fields.c:35: warning: unsigned int format, status_type_one arg (arg 2)
Bit_Fields.c:40: warning: int format, status_type_two arg (arg 2)
Bit_Fields.c:40: warning: int format, status_type_two arg (arg 2)

The Output is correct as shown below

$ ./Bit_Fields
The value of status_one is d5
The value of status_one is 48

Can anyone please tell, What the warning is about and how to resolve it?

Thanks

  • 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-17T07:03:30+00:00Added an answer on June 17, 2026 at 7:03 am

    The typical way to solve this is to have an integer-type union value, e.g:

    union status_type_one
    {
       struct status_type_one{
           unsigned delta_cts : 1;// lsb
           unsigned delta_dsr : 1;
           unsigned tr_edge : 1 ;
           unsigned delta_rec : 1;
           unsigned cts : 1;
           unsigned dsr : 1;
           unsigned ring : 1;
           unsigned rec_line : 1;// msb
       } bits;
       unsigned whole[1];       // Size should match the total bits size.
    } status_one;
    

    Now, your other code would have to change:

    status_one.bits.delta_cts=1;
    status_one.bits.delta_dsr=0;
    status_one.bits.tr_edge=1;
    ... etc ...
    

    and the print:

    printf("The value of status_one is %x\n",status_one.whole[0]);
    

    [Obviously, if the struct is more than one item in whole, you need to either loop or pass several values to printf]

    What you are doing may well appear to work, but you are not really supposed to pass a STRUCT to a printf function, and there’s no telling what that does if you use more than one machine word worth of data, or what happens on a 64-bit machine, etc, etc.

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

Sidebar

Related Questions

I'm trying to access modules but the nesting gets a bit out of hand
I'm trying to reduce access times to ClearCase a bit, and am thinking about
I'm trying to look for a unique 32-bit integer that I can access in
Hi I'm trying to access a WCF service which returns a JSON Array using
I'm trying to access the values from a CFDictionary. I've started by implementing code
I'm trying to access memory I have malloced in assembly but I keep just
I'm trying to access physical memory directly for an embedded Linux project, but I'm
I am trying to access (from a VB6 application) an unsigned 32 bit integer
I'm trying to parse returned json but can't access the data. Trying to access
I’m trying to access a certain bit of the registry however it keeps returning

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.