Thread: boolean question

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    boolean question

    Given a function definition within a project as such:

    Code:
    void usart_str_write(const char* string)
    {
         while (*string)
           Usart_Write(*string++);
    }
    If string pointer sees anything other than a null, would it return a boolean true within the while loop condition? And is null character equivalent to a boolean false?

    Thanks.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    yep.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If string is NULL, then anything can happen when you try to dereference it, so I'd make that part of the condition.

    > And is null character equivalent to a boolean false?
    0, '\0' and NULL are regarded as false in C.
    Any other integral value, any other character and any valid pointer are regarded as true.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    The reason that I'm asking the question is that I initially had *string != '\0'; as part of the condition.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That would do exactly the same thing as the code you have now - no difference in behaviour.

    --
    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. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question About Boolean
    By boylemonlaw in forum C Programming
    Replies: 1
    Last Post: 11-13-2003, 09:46 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM