Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
938 views
in Technique[技术] by (71.8m points)

c++ - Need for _Bool in C99?

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

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

1 Reply

0 votes
by (71.8m points)

_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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...