Hi
what is the difference between:
--Code:uint8_t size = 50;
andCode:for (i = 0; i < (size * 9); i++) { } // for
thanksCode:for (i = 0; i < size * 9; i++) { } // for
David
Hi
what is the difference between:
--Code:uint8_t size = 50;
andCode:for (i = 0; i < (size * 9); i++) { } // for
thanksCode:for (i = 0; i < size * 9; i++) { } // for
David
There should be no difference
wait... yes there is... something to do with the casting of the different types
Aside from the exact character content, no there's no difference (at least not in gcc, and I don't expect any other compiler to have a problem either - but there's obviously no guarantee). That assumes that the variable i is a int or at least a type larger than uint8_t, otherwise it will do something DIFFERENT from what you expect - most likely an infinite loop.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The second for-loop uses more parenthesis. Apart from that, there's no difference due to implicit cast to int.what is the difference
Greets,
Philip
EDIT: of course assuming i is of type int, as matsp suggested explicitly.
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
50 * 9 = 450
What's the max range of a uint8_t ?
When does the promotion you desire happen?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
placing the parenthesis in this case changes when the implied casting takes place, which therefore *potentially* changes what happens.
No. Let's reconsider the code in question:placing the parenthesis in this case changes when the implied casting takes place, which therefore *potentially* changes what happens.
"size" is of type uint8_t and 9 is of type int. As sizeof(int) >= sizeof(uint8_t), the result is of type int (C always uses the bigger one of the two types involved).Code:for (i = 0; i < (size * 9); i++);
As "i" is assumed to be of type int, the "<"-operator compares two ints.
The described behaviour doesn't change by omitting parentheses, as they're useless anyway: "*" has a higher precedence than "<".
Greets,
Philip
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
You are correct. In the simple example the parethesis don't make any difference. In a more complex expression though, placing () in seemingly needless places can effect the outcome when implicit type casting is occurring.
It can have that effect, and it's usually not desirable effects. Best to promote all components to the largest possible component as early as possible, to avoid any nasty side-effects. And of course, the same applies to floating point mixed with integer - it's best to make sure everything is float before even starting the calculation (e.g. by explicit casts, if need be).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.