Thread: Casting boolean as string

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Casting boolean as string

    Didn't think I'd be back again this quickly, guess my recall isn't what I'd hoped it would be.

    How do a write the value of a boolean variable to a log as a string? I'd like to see the word 'True' or 'False' in my log.

    This code is giving me a compile warning of:

    different types for formal and actual parameter 2

    because the second parameter I'm passing is a Boolean and it's expecting a string. So I guess I'm asking, how do I cast the boolean variable GbCEPSecurity as a String?

    Code:
       ShLogWrite("_CINSRCH", GbCEPSecurity);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *boolstring( _Bool b ) { return b ? "true" : "false"; }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Thanks for your response but I'm not following that at all man.

    Are you saying I need to write my own function to do this?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, you can just copy mine.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    quzah is correct, but not entirely helpful in this case

    What quzah gave you will work, but if you don't understand what his code does, it won't help you in solving the problem in the future.

    The construct
    Code:
    b ? "true" : "false"
    is a conditional expression where if the value of b (which can be an arbitrary boolean expression) is true, the value of the construct is the expression following the question mark (in this case, a pointer to a nul terminated char array containing the string "true"), and if b is false the expression has the value of the expression following the colon (in this case, a pointer to a nul terminated char array containing the string "false"). Look up "conditional expression" in your C language reference.

    Are you saying I need to write my own function to do this?
    The answer to this is "no". You can simply use a conditional expression wherever you want to do the conversion of a boolean to a string. If you do it more than once, though, a function will be more code-space efficient. You could write your code as:
    Code:
    ShLogWrite("_CINSRCH", GbCEPSecurity ? "true" : "false");

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    I've never heard of boolean variables in C?
    Thanks,
    Sahil

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C99 standard introduces them. The type is _Bool but it is aliased via a macro to bool for those who want to use it that way.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by sahil_m
    I've never heard of boolean variables in C?
    Pre-C99 versions of the C language standard don't have a built-in boolean type, but any expression that can be evaluated to a "true" or "not true" value, which usually means zero or not zero (NULL counts as zero) can be used. A simple integral variable, with no logical operators, can be considered a boolean expression, as its value is either 0 (false) or 1 (true). The addition of a boolean type in the most recent version of the standard allows for strong type checking and gives the compiler the ability to use the most efficient representation for a boolean value for the target architecture.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The type is _Bool but it is aliased via a macro to bool for those who want to use it that way.
    If you include <stdbool.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Thanks all for your responses.

    filker0, thanks for your explanation and example; exactly what I needed on both accounts.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    gives the compiler the ability to use the most efficient representation for a boolean value for the target architecture.
    What do you mean by this?
    Thanks,
    Sahil

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM