Monday, May 24, 2010

When using C++, what is the actual numerical value of a relational or logical expression that evaluates as tru

0, -1, 1, or 255? i think it is 255, but i cannot remember b/c i was hungover in class.

When using C++, what is the actual numerical value of a relational or logical expression that evaluates as tru
1.





You can do things like this:





int b = 5;


int x = ((b == 5) * 3); %26lt;-- x will be 3





Note that any non-zero value will evaluate as true, so something like this:





int x = 45;





if (x ) ... will evaluate as true.





To correct Calvin's answer, boolean variables (and all other scalar types) have no default, they must be assigned or they will have the value of whatever was there in memory.





The default for all scalar types is 0 or false, if you use the template assign:





bool x; %26lt;--- x could have any value


bool x = bool(); %26lt;-- Will assign false to x
Reply:I believe it is 1.
Reply:1 (see the code below, which has fewer characters than your question !-)





Btw, this is a question about low-level programming (that is, how a machine implements the concept of 'true'). If you are taking a course in high-level programming, you should chide your teacher.
Reply:It is 1. False is 0.





When you create a boolean variable, it's default value is 0 or false.





Easiest way to remember is that computers use 1's and 0's. This'll get rid of any other values you might think of.


No comments:

Post a Comment