0

I am new in programming I have started learning with C. I wanted to learn about the precedence of the operators in the following

if ( p == 2 || p % 2 )

Please help me.

saikat
  • 1
  • 3
  • Please check [man 1 operator](http://man7.org/linux/man-pages/man7/precedence.7.html) & try to understand the meanings of precedence and associativity. – Achal Feb 06 '20 at 05:41
  • Read C11 [§6.5 Expressions](http://port70.net/~nsz/c/c11/n1570.html#6.5), especially [§6.5.5 Multiplicative operators](http://port70.net/~nsz/c/c11/n1570.html#6.5.5), [§6.5.9 Equality operators](http://port70.net/~nsz/c/c11/n1570.html#6.5.9), and [§6.5.14 Logical OR operator](http://port70.net/~nsz/c/c11/n1570.html#6.5.14). Note that the sub-sections of §6.5 present the operators in order of precedence, higher precedence before lower precedence. The precedence partly controls how operators and operands are grouped together; it is not, however, the whole story (associativity also matters). Etc. – Jonathan Leffler Feb 06 '20 at 06:45

2 Answers2

-1

Refer Precedence Of Operators and Short Circuiting.

  • Precedence: Operator precedence determines how the operators and operands are grouped within an expression when there's more than one operator and they have different precedences.

  • Associativity: Operator associativity is used when two operators of the same precedence appear in an expression.

Since || exists, it is required that the left side of the || be evaluated first to decide if the right side of || needs to be processed. If p == 2 returns true, p % 2 will not be evaluated.

Hence p == 2 will be executed first, followed by p % 2(because % has higher precedence than ||).

The result of these 2 will then be evaluated against ||.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
AlphaGoku
  • 968
  • 1
  • 9
  • 24
  • 1
    Not my down-vote, but `p % 2` will not be evaluated first because the `||` operator strictly evaluates its LHS before its RHS (including a sequence point), and it only evaluates the RHS if the LHS is false. If the LHS is true, there's no need to evaluate the RHS. – Jonathan Leffler Feb 06 '20 at 06:31
  • 1
    Operator precedence concerns how operators are bound to their arguments (as if by parentheses) not necessarily the order of evaluation. The `||` operator does short-circuit evaluation so if the left-hand side is `true` then the the right-hand side is not evaluated. – Blastfurnace Feb 06 '20 at 06:31
  • @JonathanLeffler : Is it ok now? – AlphaGoku Feb 06 '20 at 10:47
-2

Here

if ( p == 2 || p % 2 )

it looks like

if( operand1 || operand2)

where operand1 is p == 2 and operand2 is P % 2. Now the logical OR || truth table is

operand1  operand2  operand1 || operand2
0         0         0
0         1         1
1         0         1
1         1         1

From the above table, it's clear that if the first operand operand1 result is true then the result will always true & second operand operand2 doesn't get evaluated.

operand1 is ==>

  • p == 2 (lets assume p is 2)

  • 2 == 2 results in true hence operand2 doesn't get evaluated and if blocks looks like as

    if(true) { }

Lets assume p is 3 then operand1 i.e 2 == 3 is false i.e operand2 gets evaluated i.e 3%2 i.e 1 that means if blocks looks like

if(true)
{
}

Let's assume p is 4 then operand1 i.e 2 == 4 is false i.e operand2 gets evaluated i.e 4%2 i.e 0 that means if blocks looks like

if(false)
{
}

Hope the above explanation makes sense to you.

About the associativity and precedence, please look into manual page of operator

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Achal
  • 11,821
  • 2
  • 15
  • 37
  • This doesn't talk about precedence and associativity at all. – AlphaGoku Feb 06 '20 at 06:11
  • Thanks a lot. That means any non-zero integer value of operand2 will make it it true, Isn't it like this? – saikat Feb 06 '20 at 06:33
  • The truth table doesn't enforce the rules — it means that the rules of the language make sense, but that's all. Other languages have `or` operators that always evaluate both terms — Pascal for one, Fortran (Fortran 77 at any rate) for another. Languages derived from or influenced by C tend to use short-circuit evaluation. – Jonathan Leffler Feb 06 '20 at 06:38
  • _That means any non-zero integer value of operand2 will make it it true ?_ Yes non-zero value consider as _true_ – Achal Feb 06 '20 at 07:45