Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to use '#include <stdbool.h>' in VS 2010

Tags:

I'm trying to use the stdbool.h library file in a C program. When I try to compile, however, an error message appears saying intellisense cannot open source file stdbool.h.

Can anyone please advise how I would get visual studio to recognise this? Is this header file even valid? I'm reading a book on learning C programming.

like image 611
John Avatar asked Dec 17 '11 22:12

John


People also ask

How do you fix the installation source for this product is not available?

In some cases, the restart fixes the issue. Fully Close the Program: Some programs like IDM, antivirus, VPN, and similar other third party program minimizes and run in the background. Thus when the user performs uninstallation iTunes The Installation Source for this Product is Not Available error appears.


1 Answers

typedef int bool; #define false 0 #define true 1 

works just fine. The Windows headers do the same thing. There's absolutely no reason to fret about the "wasted" memory expended by storing a two-bit value in an int.

As Alexandre mentioned in a comment, Microsoft's C compiler (bundled with Visual Studio) doesn't support C99 and likely isn't going to. It's unfortunate, because stdbool.h and many other far more useful features are supported in C99, but not in Visual Studio. It's stuck in the past, supporting only the older standard known as C89. I'm surprised you haven't run into a problem trying to define variables somewhere other than the beginning of a block. That bites me every time I write C code in VS.

One possible workaround is to configure Visual Studio to compile the code as C++. Then almost everything you read in the C99 book will work without the compiler choking. In C++, the type bool is built in (although it is a 1-byte type in C++ mode, rather than a 4-byte type like in C mode). To make this change, you can edit your project's compilation settings within the IDE, or you can simply rename the file to have a cpp extension (rather than c). VS will automatically set the compilation mode accordingly.

Modern versions of Visual Studio (2013 and later) offer improved support for C99, but it is still not complete. Honestly, the better solution if you're trying to learn C (and therefore C99 nowadays) is to just pick up a different compiler. MinGW is a good option if you're running on Windows. Lots of people like the Code::Blocks IDE

like image 96
Cody Gray Avatar answered Oct 21 '22 21:10

Cody Gray