C doesn't have a bool? Also VS2010 question

I am using VS 2010 Pro.

First, does C have no bool type? I just need to use int with 0/1. It seems odd, since most languages ​​consider boolean to be the standard type.

I also have Visual Studio 2010 Pro, but I don't have a "project C". I just created an empty C ++ project. File names end in .c

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

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

+5
source share
6 answers

C (C99) bool. stdbool.h, . , MSVC C . C89.

+12

C- (C99) bool ( _Bool, stdbool.h typedef alias bool), MSVC, . C . int, . , , .

+5

C C99.

, C (.. "" "" ). NULL "", -NULL "". , , :

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

, a == b c < d, 1 (true), 0 (false).

, -

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

, 0 1 ( , 0 1); , , - , TRUE == FALSE.

, , .

+2

bool: C "" ( - "" ). , , , :

if ((ptr = malloc(sizeof(foo))) != 0) ...

:

if (ptr = malloc (sizeof (foo)))...

C " ", "" . . , , "", , ( C99, ).

/ (, , ) -

#define BOOL int
#define FALSE 0
#define TRUE (!FALSE)

, / TRUE. . int a = 2; int b = 3;, if (a), if (b) true, a b .

: ++ bool, , , . , .c C, . , .

: : , " " ( .NET) - , , .NET - , .NET, #, VB (.NET), F # ++. (++ "", "" , , .NET, Windows API.)

- C, ?

+1

. R. answer bool.

, MSVC C99 C - ( , C99, ++), C90.

bool, , MSVC , C, ++ ++/CLI. , CLI-only, , CLI.

+1

If you work in C, I would recommend a different compiler, since VC ++ is not a modern C compiler and does not support the C99 standard. If you are running Windows, try MinGW, which basically gets you GCC with access to the Windows-y API files.

If you are using Visual Studio, create your own header file instead of stdbool.h:

#pragma once

#define false   0
#define true    1

#define bool int

I found that Visual Studio 2010 complained if I tried to use typedefinstead #definefor the definition bool.

+1
source

All Articles