+ 3

WHy does this returns 4294967293 and not +3 since %u is used, i get the reason why 4294967293 yields but why not 3 , someone ple

#include <stdio.h> int main() { unsigned i = 1; int j = -4; printf("%u", i + j); return 0; }

14th Mar 2022, 3:45 PM
blueshipswims
blueshipswims - avatar
4 Respostas
+ 3
Jayakrishna🇮🇳 how do you convert -3 to an unsigned integer?
14th Mar 2022, 4:23 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 1
There i+j result -3 Since you are expecting %u unsigned int then -3 is converted to unsigned value which is equal to what you getting...
14th Mar 2022, 4:22 PM
Jayakrishna 🇮🇳
+ 1
/* Infinity Generally negative values are stored by its complement form. But to simplify it like : Technically, 2^32 (IUNIT_MAX value) is added to it, where N is the number of bits used to represent the unsigned type. In this case, since int on your platform has a width of 32 bits, -3 is subtracted from 2^32, yielding 4,294,967,293. */ #include <stdio.h> #include <math.h> int main() { printf("%ld",(long)pow(2,32)-3); return 0; }
14th Mar 2022, 7:27 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 thanks, great explanation
14th Mar 2022, 7:39 PM
Abhishek Tandon
Abhishek Tandon - avatar