# Spot the diffrence

This is a discussion on Spot the diffrence within the C Programming forums, part of the General Programming Boards category; Hi what is the difference between: Code: uint8_t size = 50; -- Code: for (i = 0; i < (size ...

1. ## Spot the diffrence

Hi

what is the difference between:

Code:
`uint8_t size = 50;`
--

Code:
```for (i = 0; i < (size * 9); i++) {
} // for```
and

Code:
```for (i = 0; i < size * 9; i++) {
} // for```
thanks

David

2. There should be no difference

3. wait... yes there is... something to do with the casting of the different types

4. 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

5. what is the difference
The second for-loop uses more parenthesis. Apart from that, there's no difference due to implicit cast to int.

Greets,
Philip

EDIT: of course assuming i is of type int, as matsp suggested explicitly.

6. 50 * 9 = 450
What's the max range of a uint8_t ?
When does the promotion you desire happen?

7. placing the parenthesis in this case changes when the implied casting takes place, which therefore *potentially* changes what happens.

8. 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:

Code:
`for (i = 0; i < (size * 9); i++);`
"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).

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

9. 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.