The plain C equivalent of bool
Gurus,
As seen by my previous post, I am interfacing C to C++. In this endeavor a question arose as to the C equivalent of bool.
How can one define bool to be used in structures which are shared between C++ and C files? How can one guarantee that the structures have the same size in the two languages?
Maybe there is no safe solution.
Thanks.
Sincerely, Chris.
:confused:
Code:
// In some header
typedef int INT32;
#if !defined(__cplusplus)
typedef enum { false = 0, true = 1 } bool;
#endif
// Is this structure guaranteed to have the same
// size in C++ as well as C?
typedef struct
{
INT32 n;
bool b;
}
t_data;
Re: The plain C equivalent of bool
Quote:
Originally posted by dude_1967
Gurus,
As seen by my previous post, I am interfacing C to C++. In this endeavor a question arose as to the C equivalent of bool.
How can one define bool to be used in structures which are shared between C++ and C files?
You don't. You use the same types shared by both languages. Since there is no "bool" type in C, you can't use it in your structure. When you write a structures or types to be used by both C and C++, you have to use the lowest common denominator, and that's 'C'. A C++ bool can be anything from an 8-bit char, to a 64 bit int. The size of bool is implementation defined (see ANSI/ISO C++ spec, section 5.3.3.1).
Quote:
Maybe there is no safe solution.
Correct.
Regards,
Paul McKenzie
Are you using 'bool' or 'BOOL'
Which data type you are going to use BOOL or bool?
If it is bool its is simply a one byte data and is equivalent to unsigned char or unsigned __int8
It it is BOOL it is just defined as:
#define BOOL int
and nothing else.
So depending on your usage use unsigned xxx or int in your structure definition for safety meeting the same data sizes in both languages.
Re: Are you using 'bool' or 'BOOL'
Quote:
Originally posted by Ajay Vijay
Which data type you are going to use BOOL or bool?
If it is bool its is simply a one byte data and is equivalent to unsigned char or unsigned __int8
It it is BOOL it is just defined as:
#define BOOL int
and nothing else.
So depending on your usage use unsigned xxx or int in your structure definition for safety meeting the same data sizes in both languages.
See Paul MacKenzie's reply above: you cannot say that bool is equivalent to unsigned char - it's implementation-defined.
BOOL is a Windows-thing, and should be avoided by all right-thinking people whenever possible.
Re: Are you using 'bool' or 'BOOL'
Quote:
Originally posted by Ajay Vijay
If it is bool its is simply a one byte data and is equivalent to unsigned char or unsigned __int8
See section 5.3.3.1 of the ANSI/ISO spec. You cannot guarantee it is an unsigned char or an __int8. Here is the line from the specification, under the section labeled sizeof:
Quote:
Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined
Regards,
Paul McKenzie
my bool is better than thyne
Thanks for the great comments and suggestions. I think that an applicable solution can be provided for with the following code snippet below. The language elements of C are used as the lowest common denominator.
Can anyone please verify that the sizeof(my_struct) will be the same in C++ as well as C?
Thanks.
Sincerely, Chris.
Code:
typedef int INT32;
typedef enum
{
my_FALSE = 0,
my_TRUE = 1
}
my_bool;
struct my_struct
{
INT32 n;
my_bool b;
}
my_struct;
Re: my bool is better than thyne
Quote:
Originally posted by dude_1967
Thanks for the great comments and suggestions. I think that an applicable solution can be provided for with the following code snippet below. The language elements of C are used as the lowest common denominator.
Can anyone please verify that the sizeof(my_struct) will be the same in C++ as well as C?
Thanks.
Sincerely, Chris.
Code:
typedef int INT32;
typedef enum
{
my_FALSE = 0,
my_TRUE = 1
}
my_bool;
struct my_struct
{
INT32 n;
my_bool b;
}
my_struct;
In the general case, you have no guarantee that the sizeof(int) of the C compiler and that of the C++ compiler are the same. Suppose that you want to write a library that can be used both from C and C++. What happens if the C compiler targets a Win16 platform and the C++ compiler a Win32 platform?
If you want to see how to implement structures that have a well determined size, go to www.twain.org and download the twain.h file. Have a look at it, it is very instructive.