Search code examples
cunsignedsigned

Signed and unsigned don't work


I tried the following:

#include <stdio.h>

int main(void) {
  signed int a = 5;
  unsigned int b = -5;

  printf("%d\n", a);
  printf("%d\n", b);

  return 0;
}

and I get:

5
-5

So I don't understand why signed and unsigned don't work, should I get an error?


Solution

  • You have to use correct format specifiers that to get the correct result using function printf. Write

      printf("%d\n", a);
      printf("%u\n", b);
    

    The function simply interpretates internal representations of data according to the format specifiers.