Precedence,Associativity and order of evaluation

V

vjay

Hi all.Need some clarification regarding the subject(Precedence,Assoc,order
of evaluation).check this code guys
main()
{
int k=7;
if (++k/8 || ++k/9);
printf ("%d",k);
}

The output for the above program is 8.
My doubt is since unary operator ++ has the highest precedence(In the
program above) why k has not been incremented twice and then used in the
logical expression.
 
J

jacob navia

vjay said:
Hi all.Need some clarification regarding the subject(Precedence,Assoc,order
of evaluation).check this code guys
main()
{
int k=7;
if (++k/8 || ++k/9);
printf ("%d",k);
}

The output for the above program is 8.
My doubt is since unary operator ++ has the highest precedence(In the
program above) why k has not been incremented twice and then used in the
logical expression.

Within an || expression, the left hand side will be evaluated, and if
it evaluates to something different than zero, the right hand of the ||
expression will NOT be evaluated:

The expression ++k/8 will be evaluated first. Since it evaluates to one,
the next expression is not evaluated azt all.
 
M

Mark Bluemel

vjay said:
Hi all.Need some clarification regarding the subject(Precedence,Assoc,order
of evaluation).check this code guys
main()
{
int k=7;
if (++k/8 || ++k/9);
printf ("%d",k);
}

The output for the above program is 8.
My doubt is since unary operator ++ has the highest precedence(In the
program above) why k has not been incremented twice and then used in the
logical expression.

Shortcut evaluation of logical or means the second expression is not
evaluated if the first is true.

I think, and someone will correct me if I'm wrong, that the "||" makes a
sequence point.
 
S

santosh

Mark Bluemel said:
Shortcut evaluation of logical or means the second expression is not
evaluated if the first is true.

I think, and someone will correct me if I'm wrong, that the "||" makes
a sequence point.

It does.
 
S

santosh

vjay said:
Hi all.Need some clarification regarding the
subject(Precedence,Assoc,order of evaluation).check this code guys
main()
{
int k=7;
if (++k/8 || ++k/9);
printf ("%d",k);
}

The output for the above program is 8.
My doubt is since unary operator ++ has the highest precedence(In the
program above) why k has not been incremented twice and then used in
the logical expression.

Because of something known as "short circuit" evaluation. This is an
FAQ. See:

<http://c-faq.com/expr/shortcircuit.html>

Note that the code as you have typed it above invokes undefined
behaviour. To correct add the stdio.h header to supply printf's
prototype and have main() return an int value.
 
C

Chris Torek

My [question] is since unary operator ++ has the highest precedence
(In the program above) why k has not been incremented twice ...
[/QUOTE]

Because of something known as "short circuit" evaluation. This is an
FAQ. See:

<http://c-faq.com/expr/shortcircuit.html>

In addition, it is worth pointing out that:

- C does not, in a sense, even *have* "precedence and associativity",
in that the grammar in the C standards simply writes out all
the details instead of using a compressed, operator-precedence
grammar. Of course, the actual C grammar is equivalent to
various operator-precedence grammars, so a person can construct
one (or even several).

- in any case, though, "operator precedence" does *not* determine
actual runtime evaluation order. It merely tells you (the C
programmer) how the compiler must bind various operators in an
expression. It is up to the compiler to find any valid runtime
order of evaluation that computes the same answer as the expression
as-parsed.

Consider, as an example, the following code fragment:

int f(void) { puts("f()"); return 3; }
int g(void) { puts("g()"); return 4; }
...
int x = f() + g();

Which of f() or g() will be called first? Why? What if I add
an h() function and change the assignment to x:

int h(void) { puts("h()"); return 5; }
...
int x = f() + g() * h();

In what order will f(), g(), and h() be called? Why?

(Answer: the above contains what the Standard calls "unspecified
behavior". One of f(), g(), or h() will be called first, one will
be called second, and one will be called last, but it is not
specified which one, and it can change from one compiler to another,
or one compilation to the next, or even one run to the next. The
last is pretty unlikely, except in some C compilers that will be
written in the future to take advantage of multicore CPUs. So "any
of the three may be called first, as all six orders are possible,
and the reason why any particular order actually occurs is up to
the compiler.")
 
R

Robin Kåveland Hansen

vjay said:
Hi all.Need some clarification regarding the subject(Precedence,Assoc,order
of evaluation).check this code guys
main()
{
int k=7;
if (++k/8 || ++k/9);
printf ("%d",k);
}

The output for the above program is 8.
My doubt is since unary operator ++ has the highest precedence(In the
program above) why k has not been incremented twice and then used in the
logical expression.

I'd say it's because logical-or short-circuits, and when it finds ++k/8 to
be non-zero, it decides that the condition must be true, and never
evaluates ++k/9.

Similarly, logical-and should short-circuit, so if its left-hand argument
is zero, it will not care to evaluate anything on the right side.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top