Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C doesn't have a bool? Also VS2010 question

I'm using VS 2010 Pro.

First, C doesn't have a bool type? I just have to use int with 0/1. Seems odd as most languages consider boolean a standard type.

Also I have Visual Studio 2010 Pro but doesn't have a "C Project". I just created an Empty C++ Project. The file names end with .c

The problem with this is the keywords are messed up (shows bool as highlighted/valid in the editor, but compiler doesn't like it).

I went to repair/add components and they have C#, F#, C++, Visual Basic; but no C?

like image 650
user697111 Avatar asked Apr 15 '11 17:04

user697111


People also ask

Does C have no bool?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

What library includes bool in C?

The C programming language, as of C99, supports Boolean arithmetic with the built-in type _Bool (see _Bool). When the header <stdbool. h> is included, the Boolean type is also accessible as bool .

Is bool a keyword in C?

In C, bool is a macro. There is no built-in type or keyword by the name of bool in C, so typical implementations use the standard library to #define true and false to 1 and 0 respectively.

When did bool add C?

C99, the version of C released in 1999/2000, introduced a boolean type. To use it, however, you need to import a header file, so I'm not sure we can technically call it “native”. Anyway, we do have a bool type.


4 Answers

Newest C standard (C99) has bool type indeed. Just include stdbool.h and you can use it. Unfortunately MSVC does not haver proper support for C at all. Only partial C89.

like image 150
Athabaska Dick Avatar answered Oct 16 '22 16:10

Athabaska Dick


The current C language (C99) has a bool type (actually _Bool, but including stdbool.h declares a typedef alias bool for it), but since you're using MSVC, that's not available to you. In any case, using boolean types in C is completely non-idiomatic and largely useless. Just use int like everyone else. Or if you need a giant array of them, make your own bit-array implementation.

like image 27
R.. GitHub STOP HELPING ICE Avatar answered Oct 16 '22 15:10

R.. GitHub STOP HELPING ICE


C did not have an actual Boolean type until C99.

As a result, idiomatic C doesn't really use boolean-valued symbols or expressions as such (i.e., you won't see many explicit tests against "true" or "false"). Instead, any zero-valued integral expression or a NULL pointer will evaluate to "false", and any non-zero-valued integral expression or a non-NULL pointer will evaluate to "true". So you'll see a lot of code like:

foo *bar = malloc(sizeof *bar * ...);
if (bar) // equivalent to writing bar != NULL
{
   // bar is non-NULL
} 

Relational and equality expressions such as a == b or c < d will evaluate to an integral type with a value of either 1 (true) or 0 (false).

Some people introduce their own TRUE or FALSE symbolic constants by doing something like

#define TRUE  (1)  // or (!FALSE), or (1==1), or...
#define FALSE (0)  // or (!TRUE), or (1==0), or ...

Unforunately, some of those people occasionally manage to misspell 0 or 1 (or the expressions that are supposed to evaluate to 0 or 1); I once spent an afternoon chasing my tail because someone screwed up and dropped a header where TRUE == FALSE.

Not coincidentally, that was the day I stopped using symbolic constants for Boolean values altogether.

like image 2
John Bode Avatar answered Oct 16 '22 16:10

John Bode


See R.'s answer for information about the bool type.

Unfortunately, MSVC doesn't support C99 when it's compiling C code - it has bits and pieces (generally things in the C99 library that are required by C++), but for the most part it only supports C90.

As for bool still being highlighted in the editor - the highlighting in MSVC may be sophisticated, but it doesn't take into account the differentiation between C, C++, and C++/CLI. For example, if you use a construct that's CLI-only, it'll be highlighted as such even if your project has nothing to do with CLI.

like image 2
Michael Burr Avatar answered Oct 16 '22 16:10

Michael Burr