Unsigned variables. Why does the result turn out to be -1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
2 Answers
+ 2
anofris a is UINT_MAX. It will overflow int. That's the cause of your -1 result. you should be using %u not %d since your variable a is an unsigned int. Because UINT_MAX > INT_MAX, int wraps back to negative values if you exceed the signed integer max. And a is 1 more than twice of INT_MAX. https://sololearn.com/compiler-playground/cmxS5FT5ItNc/?ref=app
15th Feb 2024, 10:49 PM
Bob_Li
Bob_Li - avatar
+ 1
Thank you very much for your response. I realized my mistake. and yet thank you for your answer, it is unexpectedly accurate and complete.
16th Feb 2024, 4:49 PM
anofris