+ 1
.length() yields an unsigned integer?
I have this little code: Std::string str; For(int i = 0; i < str.length(); i++){ Std::cout << str [i] << std::endl; } When I use -Wall I got this warning: Warning: comparation between signed and unsigned integer expression [-Wsign-type] With this warning, it means that the number that yields .length() is alwayas an unsigned integer? How bad is comparing a signed and an unsigned integer? I know this is a warning and I can just ignore it, but I want to have my code as cleanest as possible, my solution is to use unsigned int in the "i" variable. But is there a better solution to this? Why . length () yields and unsigned int? Why not just a signed int?
3 Réponses
+ 2
It yields an unsigned int(bigger than zero), because how can you have a string of less than zero characters? Is it possible to have a word with negative letter values?😊
I don't receive the warning in SoloLearns playground, i suppose you are using another compiler - visual studio maybe?
+ 2
Denis Cvetanov right! Haha I didn't think about characters less than zero, it has logic now why yields an unsigned int. Well I use g++, compiling from the console on Ubuntu. I guess that having that kind of warnings are not so important, I think comparing signed and unsigned is not too bad. Thx for answer me!
0
Those warnings are a sign that your code might return unexpected results in bigger programs. But it doesn't necessarily mean it can't work that way😉. Just have it in mind when writing larger ones.
Good luck and stay hungry!😊