CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    what is the type specifier for boolean?

    In the following exmple, how 'd I use sprintf when I have a boolean type of variable?
    Code:
    char s[64];
    bool bVar = false;
    
    sprintf(s, "This is a boolean variable bVar = %?", bVar);
    What is supposed to replace ? in the sprintf statement ? Thanks.

  2. #2
    Join Date
    Aug 2009
    Posts
    440

    Re: what is the type specifier for boolean?

    Not trying to be mean, but...
    http://lmgtfy.com/?q=c+sprintf

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: what is the type specifier for boolean?

    sprintf() originates from C which didn't even have a boolean type, so there's no specifier for that. When creating C++ and making it (at least for a large part) compatible with old C code, no extra specifiers were added, since C++ has its own stream I/O facilities (<iostream> and such, you know...).
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Jul 2005
    Posts
    1,030

    Re: what is the type specifier for boolean?

    Quote Originally Posted by Alterah View Post
    Not trying to be mean, but...
    http://lmgtfy.com/?q=c+sprintf
    I did google it but didn't find the answer.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: what is the type specifier for boolean?

    Quote Originally Posted by Eri523 View Post
    sprintf() originates from C which didn't even have a boolean type, so there's no specifier for that. When creating C++ and making it (at least for a large part) compatible with old C code, no extra specifiers were added, since C++ has its own stream I/O facilities (<iostream> and such, you know...).
    If there is not boolean specifier, then what is supposed to be the specifier for boolean variable in C++? Thanks.

  6. #6
    Join Date
    Aug 2009
    Posts
    440

    Re: what is the type specifier for boolean?

    Googling "c sprintf" results in http://www.cplusplus.com/reference/cstdio/sprintf/ being first on the list. Going to that page, the first sentence reads: "Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by s."

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: what is the type specifier for boolean?

    Quote Originally Posted by LarryChen View Post
    If there is not boolean specifier, then what is supposed to be the specifier for boolean variable in C++? Thanks.
    If there is no specifier, then there is no specifier. You can't make something out of nothing, so asking "what is supposed to be the specifier" doesn't make sense. No one knows since none exists.

    If anything, you need to cast it to a type that is known by sprintf() and ensure the type cast is safe.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    Re: what is the type specifier for boolean?

    Quote Originally Posted by Paul McKenzie View Post
    If there is no specifier, then there is no specifier. You can't make something out of nothing, so asking "what is supposed to be the specifier" doesn't make sense. No one knows since none exists.

    If anything, you need to cast it to a type that is known by sprintf() and ensure the type cast is safe.

    Regards,

    Paul McKenzie
    Okey, can I do something like this,

    Code:
    char s[64];
    bool bVar = false;
    
    sprintf(s, "This is a boolean variable bVar = %d", bVar);

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: what is the type specifier for boolean?

    Quote Originally Posted by LarryChen View Post
    Okey, can I do something like this,

    Code:
    char s[64];
    bool bVar = false;
    
    sprintf(s, "This is a boolean variable bVar = %d", bVar);
    No.

    The behaviour is undefined. You can't guarantee that bVar matches the type supported by "%d". A bool's underlying type is implementation-defined -- it could be an 8-bit, 16-bit, 32-bit, 64-bit integer, you don't know. It's up to the implementation as to what a bool is underneath the hood. That's why I stated to cast bool to an integral type supported by sprintf().

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Oct 2008
    Posts
    1,456

    Re: what is the type specifier for boolean?

    a quick alternative could be simply:

    Code:
    char s[64];
    bool bVar = false;
    
    sprintf(s, "This is a boolean variable bVar = %s", bVar ? "true":"false" );

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: what is the type specifier for boolean?

    A handy little function:

    Code:
    inline const char * btoa(bool b){
         return b ? "true" : "false";
    }
    
    inline char btoc(bool b){
        return b ? 'T' : 'F';
    }
    
    inline int btoi(bool b){
        return b ? 1 : 0;
    }
    Then use %s, %c, or %d as said before.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured