Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionTo bool or not to bool in C/C++ ? Pin
Vaclav_20-Feb-20 16:11
Vaclav_20-Feb-20 16:11 
AnswerRe: To bool or not to bool in C/C++ ? Pin
CPallini20-Feb-20 21:00
mveCPallini20-Feb-20 21:00 
AnswerRe: To bool or not to bool in C/C++ ? Pin
Stephane Capo20-Feb-20 21:55
professionalStephane Capo20-Feb-20 21:55 
AnswerRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan20-Feb-20 22:00
mveRichard MacCutchan20-Feb-20 22:00 
AnswerRe: To bool or not to bool in C/C++ ? Pin
Victor Nijegorodov20-Feb-20 22:08
Victor Nijegorodov20-Feb-20 22:08 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Vaclav_21-Feb-20 10:18
Vaclav_21-Feb-20 10:18 
GeneralRe: To bool or not to bool in C/C++ ? Pin
k505421-Feb-20 10:51
mvek505421-Feb-20 10:51 
AnswerRe: To bool or not to bool in C/C++ ? Pin
Stefan_Lang21-Feb-20 0:31
Stefan_Lang21-Feb-20 0:31 
Quote:
"bool" is not "standard" C/C++ type

wrong
Quote:
when "condition" such as in "if(condition)" evaluates to true , it is binary zero

wrong
Quote:

thus if(condition == 0) would make better sense then if(condition)

wrong
Quote:
most "well written functions return x" , x being mostly zero when function is successful
when function fails – the return value is (generally) -1 or positive value identifying the error
then same as above - if(function (z) == 0) should prevail.

(Some) 'functions', yes, 'well written functions', no. To return an integer value of 0 as a sign of successfully finishing a function used to be the standard 30 years ago, because, most of the time, there was only one case of 'success' you were interested in, but potentially many cases os 'not success'. Since the type bool didn't exist back then, integers were used instead, and that opened the opportunity to use return values as error codes, where an error code of 0 was generally considered 'success'.

Then, when the first attempts were made to introduce boolean types into the language (typically impementation-dependent), some people tried to adapt to this change by declaring the integer value of 1 as indicating success in functions that only ever returned 'success' or 'failure', rather than an error code.

Unfortunately, that led to some confusion from the camp of programmers who liked the ability of returning various different error codes. This led to some unholy alliance of the different paradigms, where a value of 1, or, in fact, every positive value, was considered success, whereas negative values were considered error codes.

These different methods of returning integer values with the dual meaning of 'success/'failure', as well as an error code has survived til today. But nowadays, the correct way to report an error is throwing an exception, not returning a value! This lets you use the return value in the original sense of a function: returning the result of the operation.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

AnswerRe: To bool or not to bool in C/C++ ? Pin
Vaclav_21-Feb-20 5:30
Vaclav_21-Feb-20 5:30 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan21-Feb-20 5:54
mveRichard MacCutchan21-Feb-20 5:54 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Vaclav_21-Feb-20 8:29
Vaclav_21-Feb-20 8:29 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan21-Feb-20 22:03
mveRichard MacCutchan21-Feb-20 22:03 
GeneralRe: To bool or not to bool in C/C++ ? Pin
k505421-Feb-20 5:56
mvek505421-Feb-20 5:56 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan21-Feb-20 6:07
mveRichard MacCutchan21-Feb-20 6:07 
GeneralRe: To bool or not to bool in C/C++ ? Pin
k505421-Feb-20 6:16
mvek505421-Feb-20 6:16 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan21-Feb-20 6:18
mveRichard MacCutchan21-Feb-20 6:18 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Vaclav_21-Feb-20 8:25
Vaclav_21-Feb-20 8:25 
GeneralRe: To bool or not to bool in C/C++ ? Pin
k505421-Feb-20 9:31
mvek505421-Feb-20 9:31 
AnswerRe: To bool or not to bool in C/C++ ? Pin
leon de boer21-Feb-20 11:55
leon de boer21-Feb-20 11:55 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Vaclav_22-Feb-20 5:34
Vaclav_22-Feb-20 5:34 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan22-Feb-20 6:47
mveRichard MacCutchan22-Feb-20 6:47 
GeneralRe: To bool or not to bool in C/C++ ? Pin
leon de boer23-Feb-20 5:29
leon de boer23-Feb-20 5:29 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan23-Feb-20 6:08
mveRichard MacCutchan23-Feb-20 6:08 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Victor Nijegorodov23-Feb-20 20:43
Victor Nijegorodov23-Feb-20 20:43 
GeneralText based mining game using if and while Pin
Chleba22519-Feb-20 10:41
Chleba22519-Feb-20 10:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.