I have a string variable word=“APA”

Multi tool use
Multi tool use


I have a string variable word=“APA”



When I do the calculation: 1-word.length();
Visual studio prints 4294967294, instead it supposed to print -2.


1-word.length();



Wen I do the calculation: 1+word.length();
Visual studio prints 4, what it supposed to be.


1+word.length();



Why when I subtract length from 1 gives me that number and how can I fix it?



Note: I use C++ and Visual Studio 2012.





See the differences in the example here. Since length() returns a size_t you are performing an arithmetic operation with an unsigned type unless you cast it.
– πάντα ῥεῖ
Jun 30 at 9:02



length()


size_t





@πάνταῥεῖ good to see you're back
– Sombrero Chicken
Jun 30 at 11:09




1 Answer
1



Because the result of the expression 1 - word.length() is an unsigned type because length() returns an unsigned integer. So -2 wraps around and you get your 4294967294.


1 - word.length()


unsigned


length()


unsigned


-2


4294967294






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

4jpHNo DrPo bHl3a8qEah7VrZuZtH65vgl ikmJHBI0wIqt3U7ArWScff
5df9,yU uXPIE,6iYF

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya