- #1

- 15

- 0

I am confused by the below question.

Int A=2 B=3 C=4

B-C && (B-A) /C

I think the answer is 0 because the first part is negative.

But,my friends say the answer is 1 because -1 not mean false.

I want to ask which answer is right?

Thanks.

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- C/++/#
- Thread starter ccky
- Start date

- #1

- 15

- 0

I am confused by the below question.

Int A=2 B=3 C=4

B-C && (B-A) /C

I think the answer is 0 because the first part is negative.

But,my friends say the answer is 1 because -1 not mean false.

I want to ask which answer is right?

Thanks.

- #2

Borek

Mentor

- 28,675

- 3,166

Your friend is right - -1 doesn't mean false. 0 and only 0 means false.

- #3

CompuChip

Science Advisor

Homework Helper

- 4,302

- 47

You and your friend are both incorrect. You could easily check this by running it through a C++ compiler.

In C++, there is implicit conversion from ints to bools. && has pretty low precedence, so first the following expressions are evaluated: B - C is 3 - 4 = -1, and (B - A) / C is 1 / 4 = 0 (in integer division).

So the resulting expression is -1 && 0 which is equivalent to true && false, hence the result you get is false (0).

In C++, there is implicit conversion from ints to bools. && has pretty low precedence, so first the following expressions are evaluated: B - C is 3 - 4 = -1, and (B - A) / C is 1 / 4 = 0 (in integer division).

So the resulting expression is -1 && 0 which is equivalent to true && false, hence the result you get is false (0).

Last edited:

- #4

Borek

Mentor

- 28,675

- 3,166

Share: