Simple Explanation Needed

This is a discussion on Simple Explanation Needed within the C Programming forums, part of the General Programming Boards category; When something like this is done in C: Code: int i; const static char phase[] = "urrrddd"; for (i = ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question Simple Explanation Needed

    When something like this is done in C:

    Code:
    int i;
    const static char phase[] = "urrrddd";
    
    for (i = 0; phase[i]; i++)
    {
        /* some code */
    }
    is phase[i] a shorter way of saying: phase[i] != 0
    or something else? Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Pretty much.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    58
    When you don't specifiy a condition it evaluates to true if it is different than zero so phase[i] and phase[i]!=0 are the same thing when already compiled.

    And that loop will run while the string isn't terminated.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Boolean expressions in C are numerical expressions as well, and they are resolved in the following way: Every number and expression that results to a non-zero value is true, while every expression that results to zero is false.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    49

    Question

    Perfect, thank you for all the explanations.

    One more simple question that is similar.

    Code:
    int multi_tile, norec, opt_DM;
    
    /*some code*/
    
    multi_tile = norec && opt_DM < 32;
    For the code above, is there a long way of coding that last line...maybe with an if statement? I'm trying to get a better idea of how '&&' and '<' are being used to give the value of multi_tile. Thanks for the help
    Last edited by slowcoder; 07-10-2007 at 07:10 AM.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    58
    Quote Originally Posted by slowcoder View Post
    Perfect, thank you for all the explanations.

    One more simple question that is similar.

    Code:
    int multi_tile, norec, opt_DM;
    
    /*some code*/
    
    multi_tile = norec && opt_DM < 32;
    For the code above, is there a long way of coding that last line...maybe with an if statement? I'm trying to get a better idea of how '&&' and '<' are being used to give the value of multi_tile. Thanks for the help
    multi_tile will be 1 only if norec is different than zero and opt_DM is less than 32.

    if those conditions are not true multi_tile will be zero.

    Boolean expresions evaluate to 1 or 0, indicating true and false.
    Last edited by Govalant; 07-10-2007 at 10:21 AM. Reason: clarification

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    49
    So just to verify that I interpreted that properly, this would be the equivalant?

    Code:
    int multi_tile, norec, opt_DM;
    
    /*some code*/
    
    if ((norec != 0) && (opt_DM < 32))
       multi_tile = 1;
    else
       multi_tile = 0;
    Thank you Govalant!

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    58
    Exactly =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. strcpy syntax explanation needed
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2002, 07:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21