Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 556 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logical Expressions in C misunderstanding

#1
So, I was writing some code and I was getting an unexpected output in one part of my program which disrupted the entire system.

I managed to extract and simplify the problem to a basic logical expression. Let's say:

int i = 1, j = 1, k = 0;

printf("%d\n", ++i || ++j && k);
printf("%d, %d, %d\n", i, j, k);

return 0;

The output for this program is:

`1`

`2` `1` `0`

I am thinking that the value of `j` was not incremented to `2` due to the short circuit nature of the `||` operator. However I am confused how the value of the first `"%d"` is `1`. Shouldn't the value of `k` be non-zero for the `&&` statement to return `1`? Or isn't this statement executed at all since `++i || ++j` is not `0` and thus returns a `1`?

- I know that `&&` is a logical *and*, and *expr1* && *expr2* has the value 1 if values of *expr1* && *expr2* are **both** non-zero.

Any clarifications would be appreciated and please excuse the basic nature of this question.
Reply

#2
`++i || ++j && k` is evaluate to 1 (true) because i equals 2 (++1), (++j && k) isn't evaluated because short circuit.
Reply

#3
From the C Standard (6.5.14 Logical OR operator)

> 3 The || operator shall yield 1 if either of its operands compare
> unequal to 0; otherwise, it yields 0. The result has type int.

This expression

++i || ++j && k

is equivalent to

++i || ( ++j && k )

and according to the quote from the Standard the expression returns integer value 1 because `++i` is not equal to zero. The subexpression `( ++j && k )` is not evaluated.

Reply

#4
Operators precedence. `&&` has higher precedence than `||`.

Your expression is the same as: `++i || (++j && k)`

`++i` is `TRUE`, the parenthesis is not evaluated anymore.
Reply

#5
`&&` has higher precedence than `||`.
(See

[To see links please register here]

)
so

++i || ++j && k
is

++i || (++j && k)

and `||` shortcircuits if the first operator is truthy, as per [6.5.14p4][1] .

If you're on `gcc` or `clang` and compile your code with `-Wall`, the compiler will nudge you to put those parentheses there. It's probably a good idea to heed that advice, as some people get confused by the precedences (I hear).


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through