-
December 11th, 2012, 05:11 PM
#1
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.
-
December 11th, 2012, 05:51 PM
#2
Re: what is the type specifier for boolean?
Not trying to be mean, but...
http://lmgtfy.com/?q=c+sprintf
-
December 11th, 2012, 08:54 PM
#3
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.
-
December 12th, 2012, 09:29 AM
#4
Re: what is the type specifier for boolean?
Originally Posted by Alterah
I did google it but didn't find the answer.
-
December 12th, 2012, 09:34 AM
#5
Re: what is the type specifier for boolean?
Originally Posted by Eri523
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.
-
December 12th, 2012, 10:33 AM
#6
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."
-
December 12th, 2012, 03:12 PM
#7
Re: what is the type specifier for boolean?
Originally Posted by LarryChen
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
-
December 12th, 2012, 05:50 PM
#8
Re: what is the type specifier for boolean?
Originally Posted by Paul McKenzie
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);
-
December 12th, 2012, 07:08 PM
#9
Re: what is the type specifier for boolean?
Originally Posted by LarryChen
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
-
December 13th, 2012, 03:03 AM
#10
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" );
-
December 13th, 2012, 09:18 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|