views:

191

answers:

5

I haven't done much c programming but when I do when I need a false I put 0 when I want true I put 1, (ex. while(1)), in other cases I use things like "while(ptr)" or "if(x)".

Should I try using C99 booleans, should I recommend them to others if I'm helping people new to programming learn c basics(thinking of cs 1?? students)?

I'm pretty sure the Visual Studio compiler supports c99 bools, but do a lot of projects (open source and c apps in industry) compile for c89? If I don't use C bools should I at least do something like #define TRUE 1 #define FALSE 0?

Also what about c++ Booleans (for c++)?

+3  A: 

Yes, you should use the language abstractions when they are available. When I use an older C compiler I still create some abstraction of a bool. Using literals in your code is a very poor practice.

Amardeep
Generally I agree about literals, but in this case `0` is essentially C's way of spelling "false".
T.E.D.
Yeah, I can give 0 a pass since it is also a legit way to represent NULL.
Amardeep
+5  A: 

In C++ there is no reason to not use it. In C, i only use int for this task, without any #define or something like that. Variable-names like isDefinition are clear enough to indicate what's going on, in my opinion.

Of course, there is nothing wrong with defining your own bool or using the ones of <stdbool.h>.

Johannes Schaub - litb
There is something wrong with defining your own instead of using the standard header, unless you have a good reason for doing it.
Stephen Canon
@Stephen i suspect this good reason would be portability for most :)
Johannes Schaub - litb
+2  A: 

C++ booleans are fine as they are part of the language and are supported by basically any C++ compiler these days.

C99 booleans seem like a good idea but just keep in mind whether the code you write today will ever need to be used in a C89 project...

Justin Ethier
And even then it's not difficult to define the right types in a header.
tristopia
+1  A: 

The compiler can do better optimization when it knows a variable is boolean. Also when using ints it's easier to introduce bugs when used in a bitwise context, as ints can be inadvertently set to values other than 1

pixelbeat
+1 for bringing this up. However, non-zero is essentially C for "true". Anybody mixing bitwise and boolean operators on purpose deserves what they get. Anybody doing it on accident is yet another sad victim of C's language designers.
T.E.D.
+1  A: 

Use bool in C++. It has been there for years and every C++ compiler supports it, Use it in C if your code requires other C99 features. Don't use it in pre-C99 code, since any non-zero value will be interpreted as true, and using defines may lead to bugs that are hard to track down (some C library functions are documented to return any non-zero int value, and even if it's generally poor practice to write something like

if (var==TRUE) { ... }

things like this may break, and might even behave differently under different compilers/operating systems.

Axel