-5

1.The code defines the variables as unsigned integer however the output is shown negative.

#include<stdio.h>
#include<limits.h>
#include<stdint.h>
int main(){
    uint32_t a= 25,b=50;
    a = a-b;    
    printf("\n%d\n",a);
    return 0;
}
GANESH B K
  • 381
  • 3
  • 5
  • It's because you told `printf` to print a `%d`, which is a signed integer. That's what `%d` means. A computer always does exactly what you tell it to do, instead of what you want it to do. – Sam Varshavchik Jan 26 '18 at 03:56

1 Answers1

1

The correct format specifier would be (The macro PRIu32 is defined in inttypes.h header ).

printf("%" PRIu32 "\n",a);

%d expects the arguments passed to it would be address of an signed int - that's why the value you passed is considered as signed int. After you have used the correct format specifier the output would be UINT32_MAX+1-25.

Using the wrong format specifier is undefined behavior.

user2736738
  • 30,591
  • 5
  • 42
  • 56