The RHS defaults to signed int evaluation, which DOES overflow. The compiler does not lie.
Either cast a constant on the RHS to unsigned int, or append "UL", to force unsigned long, which you will need for seconds per day (86400 of them) in any case.
jremington:
The RHS defaults to signed int evaluation, which DOES overflow. The compiler does not lie.
Will the compiler 'pre-compile' the result or execute the math when the time comes?
jremington:
Either cast a constant on the RHS to unsigned int, or append "UL", to force unsigned long, which you will need for seconds per day (86400 of them) in any case.
I believe that is what is called an assumption. The OP is why the compiler throws a warning on a value that is not beyond the limits of the data type. The exact code and error are posted.
The value 64800 is recognised as a uint16_t.
It won't overflow if you add one to it.
No problem.
What I don't understand is why you initialise the variable to zero first. I can only hope that for one on such a tight memory budget, the compiler optimises that away.
The OP is why the compiler throws a warning on a value that is not beyond the limits of the data type.
The correct answer was given in the first line of reply #1. You can attempt to continue with your pointlessly adversarial responses, but it won't get you any further.
TheMemberFormerlyKnownAsAWOL:
The value 64800 is recognised as a uint16_t.
It won't overflow if you add one to it.
The type will be long, not uint16_t (at least on a platform where int is 16 bits).
You can find the rules for integer literals here: Integer literal - cppreference.com
Unless you explicitly add a u suffix, decimal integer literals are never unsigned.
Hexadecimal, octal and binary literals may be unsigned, depending on their value, e.g. the type of 0x8000 is unsigned int, but 32768 (the same number in decimal) has type long int.
Thanks for the correction - what I meant to write was "The value 64800 is recognised as capable of fitting in a uint16_t without overflow", but Christmas Eve being Christmas Eve, and beer being beer...
xxx.ino:263:21: warning: integer overflow in expression [-Woverflow]
total_time = 186060;
I thought the maximum value of uint16_t was 65535? 18*60*60 = 64800. What gives?
The expression 186060 is not unsigned. I mean - it gets assigned to an unsigned variable, but that's right at the end of the process. The multiplication is a signed multiplication.