zer0uno
zer0uno

Reputation: 8030

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?

Upvotes: 0

Views: 83

Answers (2)

Nullpointer
Nullpointer

Reputation: 1086

Use Format spcifier of unsigned int which is %u

Now compile and run the code you will see the difference

 #include <stdio.h>

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

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

      return 0;
    }

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

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.

Upvotes: 3

Related Questions