Is it valid to cast a char as a bool?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a function that returns a bitfield.

I later pass this variable to other functions that require only a boolean. In these functions all that is needed is whether the variable is zero or non zero. Is casting a variable as a boolean a valid cast?

E.g.

uint8_t Wibble = 0x36;

some_function((bool)Wibble);

//Prototype
void some_function(bool b_var);

Ben
-Using IAR (& ocasionally CodeVision)
0.7734
1101111011000000110111101101

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well it's a pretty fair bet that a bitfield variable (I presume a :1 ?) can only possibly hold 0 or 1 so you can cast it to pretty much anything you like and it'll be expanded out to fit the target. But are "bool" efficient? I thought they may actually be held in 16 bits so you are wasting 8.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"boolean" or "bool" is not a C keyword.

You will have to look at your compiler to see how it handles it. Probably a typedef to a char or an int or an enum. Or possibly a "bit" which is not a C keyword.

So you can obviously use whichever Compiler extensions are provided. But do not expect your code to be portable.

If I was you, I would play safe and use:

some_function(Wibble != 0);

or you could use a macro that explicitly casts Wibble to TRUE or FALSE.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

some_function(Wibble != 0);

I think the GCC compiler's code uses the odd construct of:

some_function(!!Wibble);

Which I personally think is damn ugly. If the function will internally check if the value is true (non-zero in C) you can avoid the casting/altering altogether.

The C99 standard defines the new type _Bool which can be internally implemented however the compiler likes. On GCC this takes the form of a char, but the compiler will cast to a definite 0 or 1 value from other types.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
On GCC this takes the form of a char,

I stand corrected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I think the GCC compiler's code uses the odd construct of:

some_function(!!Wibble);

Which I personally think is damn ugly


But this is because there really isn't a bool, it is a char masquerading as a bool. This construct is simply a double NOT. If the value of Wibblle is not 0, the first not makes it false (which is a 0), the second then makes it true (which is a 1). For Wibble starting at 0, it of course goes back to 0. This is what guarantees an official bool value. If you are using stdbool (or C99), then there is no need to do this yourself, just an explicate or implicate cast will do it for you.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But this is because there really isn't a bool, it is a char masquerading as a bool. This construct is simply a double NOT. If the value of Wibblle is not 0, the first not makes it false (which is a 0), the second then makes it true (which is a 1). For Wibble starting at 0, it of course goes back to 0. This is what guarantees an official bool value. If you are using stdbool (or C99), then there is no need to do this yourself, just an explicate or implicate cast will do it for you.

Oh yes I know what it is doing and why it is doing it -- I just happen to think it's damn ugly. I suppose it's a necessary evil on non C99 compilers, however.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I just happen to think it's damn ugly

I guess beauty is in the eye of the beholder - I thought that looked very neat and clever myself.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

certainly shorter than (Wibble!=0) Which while more explicate and obvious, can get tiring to write... and we all know how programmers like to take shortcuts ;)

Writing code is like having sex.... make one little mistake, and you're supporting it for life.