C doesn't have a bool? Also VS2010 question

前端 未结 6 1827
礼貌的吻别
礼貌的吻别 2021-01-12 16:17

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.

相关标签:
6条回答
  • 2021-01-12 16:45

    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.

    0 讨论(0)
  • 2021-01-12 16:48

    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.

    0 讨论(0)
  • 2021-01-12 16:51

    Concerning the bool type: In C, any non-zero value is regarded as "true" (and zero is "false"). This comes in handy when, say, checking the value of a pointer:

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

    can be shortened to:

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

    C was designed to be a "mid-level" language, i.e. in-between assembler and traditional "high-level" languages. It was also designed to be compact/concise. So it has a minimalist flavor, exemplified in the its support for "shorthand" like the above, and also in the omission of a built-in Boolean data type (up to C99, as others have pointed out).

    Many libraries/frameworks (ones that I'm aware of anyway) do something like the following

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

    This does mean that you should avoid directly comparing values/results to TRUE. Consider the following. Given int a = 2; int b = 3;, then both if (a) and if (b) evaluate to true, but a and b are not equal.

    Concerning syntax highlighting: C++ does have a bool type, which I'm guessing is why the compiler highlights the word. However, the fact that your source file ends it .c marks it as C code, so the type isn't allowed. Seems like the syntax highlighting should catch this, though.

    Concerning the absence of C components: If I understand the question correctly: the short answer is, in order to do "managed code" (ie .NET) development -- which is what you'd have to be doing in order to use .NET components -- you need to use a language supported by the .NET runtime, i.e. C#, VB(.NET), F#, or C++. (C++ is available in both "managed" and "unmanaged" flavors, meaning you can develop either against .NET or the Windows API.)

    Are you under some sort of directive to use C as opposed to other languages?

    0 讨论(0)
  • 2021-01-12 16:54

    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.

    0 讨论(0)
  • 2021-01-12 16:57

    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.

    0 讨论(0)
  • 2021-01-12 17:03

    If you're developing in C, I'd recommend a different compiler as VC++ is not a modern C compilier and does not support the C99 standard. If you're on windows try MinGW, which basically gets you GCC with access to Windows-y API stuff.

    If you're set on using Visual Studio, create your own header file to use 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 a typedef instead of a #define to define bool.

    0 讨论(0)
提交回复
热议问题