Thread: Spot the diffrence

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    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. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    There should be no difference

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    wait... yes there is... something to do with the casting of the different types

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    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.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    placing the parenthesis in this case changes when the implied casting takes place, which therefore *potentially* changes what happens.

  8. #8
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    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
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  2. how do I move cursor to a certain spot on the screen (x,y) ?
    By freeman_peterso in forum C Programming
    Replies: 5
    Last Post: 07-11-2003, 12:30 PM
  3. Whats the Diffrence
    By coolazz0 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-19-2003, 10:49 PM
  4. Writing to specifed spot in EDIT box(I REALLY NEED ANSWER)
    By SyntaxBubble in forum Windows Programming
    Replies: 0
    Last Post: 11-12-2001, 05:36 PM
  5. How do I put text in a specified spot of an EDIT box?(PLEASE READ!)
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 11-10-2001, 07:55 AM