Click to See Complete Forum and Search --> : The plain C equivalent of bool


dude_1967
March 29th, 2003, 01:20 PM
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:



// 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;

Paul McKenzie
March 29th, 2003, 05:34 PM
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).Maybe there is no safe solution.Correct.

Regards,

Paul McKenzie

galathaea
March 29th, 2003, 08:05 PM
I thought c99 had added a boolean in its little underscore frenzy, something like _Boolean or something like that. I could be wrong, and I don't have any c related books dating very far into the 90s, but in the recesses of my mind I remember seeing that...

Graham
March 30th, 2003, 04:28 AM
I read something by, I think, Herb Sutter a while back on the subject of C99's "bool": it's not a real type (i.e. they haven't added a new fundamental type to the language) - I think it's just a typedef.

Ajay Vijay
March 30th, 2003, 04:48 AM
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.

Graham
March 30th, 2003, 05:14 AM
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.

Paul McKenzie
March 30th, 2003, 07:12 AM
Originally posted by Ajay Vijay
If it is bool its is simply a one byte data and is equivalent to unsigned char or unsigned __int8See 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:

Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined

Regards,

Paul McKenzie

dude_1967
March 30th, 2003, 02:17 PM
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.



typedef int INT32;

typedef enum
{
my_FALSE = 0,
my_TRUE = 1
}
my_bool;

struct my_struct
{
INT32 n;
my_bool b;
}
my_struct;

Gabriel Fleseriu
March 31st, 2003, 01:29 AM
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.



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.

dude_1967
March 31st, 2003, 02:56 AM
Thanks Gabriel,

Came to the same conclusion shortly after writing the post. The use of enum is not safe over multiple compiler platforms. Thanks for the link. I'll take a look.

By the way, does anyone know if sizeof(enum) = sizeof(int) is specified?

Thanks again, everyone. This was a very informative post.

Sincerely, Chris.

:eek:

Gabriel Fleseriu
March 31st, 2003, 03:58 AM
From ANSI/ISO C++, C 1.5.7.2.6:

Change: In C++, the type of an enumerator is its enumeration. In C, the type of an enumerator is int.
Example:

enum e { A };
sizeof(A) == sizeof(int) // in C
sizeof(A) == sizeof(e) // in C++
/* and sizeof(int) is not necessary equal to sizeof(e) */

galathaea
March 31st, 2003, 05:04 AM
Just to add to Gabriel's post, instances of one enum type are not even guaranteed to be of the same size as instances of other enum types! Its a weird c++ism which allows a compiler to optimize for size or speed or whatever as long as the enum type has instances large enough to hold its largest value...

Alexis Moshinsky
March 31st, 2003, 10:56 AM
I think there is a way to make bool of the same size in C and C++

#define bool Boolean
typedef char Boolean;

(ugly, but possible :D )