You are not logged in.
Compatible in what manner? You mean storage size? Like if you wrote out a "bool"
to a file (or sent it over a socket) in a C app, and then read it into a "bool" in a C++
app, would it work? I suspect it probably is compiler dependent, and you'd be wise
to not assume they're identical... Best to just use a simple "char" (or "int8_t") for
cross-platform interchange like that... However, I wouldn't be surprised if most/all
systems/compilers treat them both identically... It'd be rather pointless to implement
"bool" any alternate way... (Unless maybe you had a bunch together in a single
location, and then maybe a smart compiler might turn them into bit fields... Eg: to save
space in a struct...)
I was thinking about such situation:
I create a library in C, where I expose function:
bool some_function (bool some_arg);
Later I would like to use this library in C++ program. Should it work?
I suspect it depends on your system and compiler... I believe it will work on a glibc
system with GCC/G++... Take a look at your local <stdbool.h>... In mine, it does this:
#ifndef __cplusplus #define bool _Bool #define true 1 #define false 0 #else /* __cplusplus */ /* Supporting <stdbool.h> in C++ is a GCC extension. */ #define _Bool bool #define bool bool #define false false #define true true #endif /* __cplusplus */
So, it seems this is a GCC/glibc trick, and may not necessarily work on other
Also, since the real C99 type is "_Bool" with "bool" just being a macro cover for it,
a C++ compiler probably won't know anything about it without trickery like the
So, in short, unless you know your target system/compiler supports it, you'd probably
be better off avoiding it and using a simple "char" or "int" or something instead...
C++ still supports C-style handling by converting any non-zero int value into "true"
and zero into "false" for its bools, and doing the inverse conversion of them back
to ints as needed, so there'd be nothing stopping you from treating a function that
returned or took as an arg "char" or "int" from being assigned to a C++ "bool" or
passing one to it as an arg...
About the only feature real (C99 or C++) bools give you is turning all non-zero values
into the exact value 1... But, honestly, I'm not sure how useful such a feature really
is... Personally, that feature has annoyed the hell out of me on more than one occassion
before... (Eg: wanting to add a third condition to an existing "bool"-based API, without
needing to break backward compatability... Such that you can pass or it can return
some value other than 0/false or 1/true to get some alternate behavior... With a standard
int or char, it's trivial of course; either just use 2 or -1 or whatever... People unaware
of this behavior will still treat the value as true, for all practical purposes, but those
who are aware can look for that specific value and behave differently from the 1/true
case, if they choose... Yes, technically speaking this is a kluge, and what you're
dealing with then is no longer a boolean value... But, still, such kluges are sometimes
still useful... A good example is a verbosity or debug level: every time you add "-v"
or "-d" to the command-line, it increments the level... Most places can just test it
like a bool to see if it's non-zero, while specific places can test for specific values
and only output stuff if the value is high enough...)