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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:26:42+00:00 2026-06-06T09:26:42+00:00

Possible Duplicate: Unsigned long with negative value Assigning negative numbers to an unsigned int?

  • 0

Possible Duplicate:
Unsigned long with negative value
Assigning negative numbers to an unsigned int?

#include<stdio.h>

int main()
{
    struct a 
    {
        unsigned int i:3;
        int c:3; 
    }s;
    s.i=5;
    s.c=5;

    printf("s.i=%d\n",s.i);
    printf("s.c=%u\n",s.c);
    unsigned int x = -1;
    printf(" x = %d", x);
    return 0;
}

This outputs:

s.i=5
s.c=4294967293
x=-1 

I am not clear about the output of “x” and “s.c”(s.c can store number of 3 bits only but in the output it is giving very large value).”x” is declared as unsigned so the bits stored in x are 1111111……. and the output of x should be a large value instead of -1. 1st printf() statement is giving the result as expected
I am using devc++ compiler.

  • 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-06T09:26:44+00:00Added an answer on June 6, 2026 at 9:26 am

    It seems there are two things going on that need to be understood:

    • printf() conversion specifiers
    • Integral conversions

    And two more things that help make sense of your output:

    • Argument promotion for variadic function arguments
    • Two’s complement representation

    First, printf() is a variadic function. It doesn’t know what the types of its arguments are (other than the format string), so you have to use conversion specifiers to tell it how to interpret the arguments. These arguments are subject to the "default argument promotions" such that your 3-bit bit fields are being promoted to ints.

    You are using conversions specifiers (%d, %u, and %d) that do not match the signedness of your data, so you will get undefined behavior that depends on how your data is actually represented in memory.

    Second, the C11 standard states:

    6.3.1.3 Signed and unsigned integers

    • When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

    • Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

    • Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

    (As far as I can tell, the details relevant here have been true at least since C89.)

    This tells us a couple things about your code:

    • When you assign -1 to an unsigned int, UINT_MAX + 1 is added to it, giving UINT_MAX, or 4294967295 for 32-bit integers.

    • When you try to assign 5 to a 3-bit signed bit field, the result is implementation-defined.

    So you’ve got both undefined and implementation-defined behavior, but we can still try to make sense of your output, just for fun. I’m assuming 32-bit integers and two’s complement representation.

    Your system represents the 4294967295 stored in x as 11111111 11111111 11111111 11111111. When you told printf() that the argument you were passing it was signed, those same bits are interpreted as -1, which is the output you got.

    For s.c, the implementation-defined behaviour you seem to have gotten is straightforward:
    the three bits 101representing 5 got stored as-is. That means that with the correct conversion specifier, printf() should show s.c as -3.

    Here are the values you’ve assigned:

    s.i = 101
    s.c = 101
      x = 11111111 11111111 11111111 11111111
    

    The 3-bit values are promoted to 32-bit by left-padding with 0 for the unsigned value and repeating the sign for the signed value:

    s.i = 00000000 00000000 00000000 00000101
    s.c = 11111111 11111111 11111111 11111101
      x = 11111111 11111111 11111111 11111111
    

    Which, when interpreted as signed, unsigned, and signed integers gives:

    s.i=5
    s.c=4294967293
    x=-1
    

    The x=-1 suggests to me that you are in fact using a two’s complement representation (which was a pretty safe bet, anyway), and the output for s.c suggests that your ints are 32 bits wide.

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

Sidebar

Related Questions

Possible Duplicate: How to printf “unsigned long” in C? I have my number like
Possible Duplicate: What does 'unsigned temp:3' means struct Test { unsigned a : 5;
Possible Duplicate: Addition of two chars produces int Given the following C++ code: unsigned
Possible Duplicate: What does 'unsigned temp:3' means I don't understand this struct definition. It
Possible Duplicate: What does template <unsigned int N> mean? Hi ! Are non-type template
Possible Duplicate: What does 'unsigned temp:3' mean? please what does this notation mean int
Possible Duplicate: What does 'unsigned temp:3' means what does this mean in c int
Possible Duplicate: Convert a long hex string in to int array with sscanf I
Possible Duplicate: Print leading zeros with C++ output operator (printf equivalent)? #include <iostream> #include
Possible Duplicate: Unsigned and signed comparison unsigned int and signed char comparison I have

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.