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;
}
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;
}
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.