This question already has answers here:
Does true equal to 1 and false equal to 0? [duplicate]
(2 answers)
Closed 4 years ago.
I recently have been reading a tuition book in C++, the question was under a chapter discussing Boolean operators. The question that confused me was as follows:
Which of the following is true?
A. 1
B. 66
C. .1
D. -1
E. All of the above
The answer itself is E according to the paper however, from a newbie perspective like myself, I assumed that A was the answer as a true value is stored as a 1 whereas a false value was stored as a 0? So why would the answer be all of the above?
Any value that is not equal to zero is considered true. So the answer to the question is E since none of the listed values are zero.
Related
This question already has answers here:
Double Negation in C++
(14 answers)
Closed 2 years ago.
Came across using !! in C++ during condition check
if ( !! (flag != 0 )){..
}
This could be directly used like
if( flag != 0 ){..
}
Is there any specific corner use case in C/C++ or is it just a form of coding style ?
In this case, it's superfluous, but in general this style is used to convert the actual expression value to
an integer type in C.
a boolean type in C++.
This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 2 years ago.
Lets assume a simple If Statement with two conditions A and B:
If ( condA && condB)
Is the Sequenz for all compilers the Same?
condition A
condition B
And is the execution of condition B therefore optional, in case condition A is already false?
Yes. Not evaluating condition B if A is false is called short circuit logic, and this behavior is guaranteed by the language specification.
This question already has answers here:
Strange label usage for an IF condition in a DO loop [duplicate]
(2 answers)
Closed 7 years ago.
Learning old Fortran77-codes full of "goto-spagetti", I met with the following instruction:
if(condition) label1, label2, label3
where labels are just three numbers. Has anybody any ideas what it means?
Arithmetic if. It goes to
label1 if the expression is negative
label2 if the expression is 0
label3 if the expression is positive.
Caveat: If you use it with floating point expressions, note that a result that should be 0 might be "a small number close to 0" due to rounding errors. This limits the usefulness of the arithmetic if.
This question already has answers here:
Strange label usage for an IF condition in a DO loop [duplicate]
(2 answers)
Closed 9 years ago.
I have a question about some code I'm looking at written in Fortran. The section of the code I'm confused about is written below.
DO 40 LL=1,N
DO 40 I=1,N-1,2
IF((LL-I)*(LL-I-1)*(LL-I*2)*(LL-I+N-2)) 22,21,22
NODO=LL-I+1
IF((LL.EQ.1) .AND. (I.EQ.N-1)) NODO=NODO+N
I don't understand the condition for the first IF statement. It just looks like numbers are being multiplied together but that number isn't checked against anything. Then 3 line numbers are written after the IF statement. Do anyone know what this IF statement is doing? The last IF statement makes sense as a condition is actually being checked. Thanks.
The line
IF((LL-I)*(LL-I-1)*(LL-I*2)*(LL-I+N-2)) 22,21,22
is an arithmetic if statement, which is certainly obsolescent (the Fortran standard term for deprecated) and may even have been removed in the latest language standard(s). If the condition evaluates to a negative number program control branches to the line with the first label (ie 22), if it evaluates to 0 to the second label (21), if to a positive value to the third label (22). As you see the three labels need not all be different.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does 'unsigned temp:3' mean?
I saw some c++ code today that used single colons.
bool variable_name : 1;
what is the difference between this and
bool variable_name = true;
The ": 1" means it is a bit field with 1 bit, or at least that's what it means in C. It probably was put there to save some memory, allowing multiple bools to be stored in the same byte. The downside is that you probably can't make a pointer to that bool.