Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am reading a book on C. It says that C99 added a data type _Bool. It is basically an int but stores only 0 or 1. Now I do not understand why there is a need of such a data type. We already have bool which implicitly translates to int and vice versa. So can somebody please tell me a situation where such a data type would be useful.

PS: C++ does not seem to support such a data type as seen here.

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    _Bool b = false;
    if(b == 0)
        printf("FALSE");
    else
        printf("TRUE");
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
928 views
Welcome To Ask or Share your Answers For Others

1 Answer

_Bool in C doesn't have the same semantic than the other integer types.

For example, for the conversions to integers:

 (_Bool) 0.5

evaluates to 1

whereas

 (int)  0.5

evaluates to 0.

(This is the example given by the C99 Rationale for the _Bool type).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...