Why t he value of unsigned inter is displayed as signed?

Debojit Acharjee 435 Reputation points

In the following program -1 was assigned to unsigned variable 'a' and 'b' but why the value of 'a' is printed as -1? The value of 'b' is also not printed as 1 even after using the unsigned specifier '%u".

#include <stdio.h>

int main ()
  unsigned int a;
  unsigned int b;

  a = -1;

  b = a;

  printf ("Signed value of a: %d\nUnsigned value of b: %u", a, b);

  return 0;



Signed value of a: -1

Unsigned value of b: 4294967295

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,615 questions
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,626 questions
0 comments No comments
{count} votes

Accepted answer
  1. Barry Schwarz 2,511 Reputation points

    When you lie to printf, what do you expect?

    You told printf to print the value of a using %d as the format specification. Go to your library reference and learn what type of value %d requires. Did you provide a value of that type? Obviously not.

    printf printed the correct value for b. Go to your language reference and learn what happens when you assign a value that is out of range to an unsigned integer.

    You are asking a lot of questions about why C does not do what you expect. It is time to realize that many of your expectations are not based on what the standard requires the language to do.

    3 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful