inr main ()
{
int x=3,y=4,z=4;
printf("%d",z>=y>=x),

}


the answer is zero ..how??

Because what you think is happening isn't.

The comparison is carried out left to right :

Result
z>=y 4>=4 1 (TRUE)
1>=x 1>=3 0 (FALSE)

hence the result of 0

ya , but as per the graduate lectures , printf evaluation will be done right to left , but in this case how come left to right takes place ???

>>printf evaluation will be done right to left

I think you misunderstood the lecturer.

Look :
Solving the thing right to left -
y>=x which means 4>=3 is true. So it's 1. And as it is 1, then preceding to the next set of instruction which is z>=1 (as 1 was the result of that expression), 4>=1 is not true. So you get a 0.
And yes, it would be performed right to left.
Hope you understand what i said. Thanks :)

>>,z>=y>=x

The evaluation is always left to right, not right to left. Z >= y, or 4 >= 4 is true, and 4 >= x or 4 >= 1 is false, so the result is 0.

4>=1 is not true. So you get a 0.

4 >= 1 is false, so the result is 0.

Since when? Have they changed something since I've been in school?

What is apparent is that there are differing views on what the evaluation. The solution is simple, if there is any ambiguity then use brackets.

Since when? Have they changed something since I've been in school?

You are right, 4>=1 is true. But I think what is going on is that the program is comparing true (1) with the value of x, which is 3.
Z>=y is true (1)
1 >= x is false

What is apparent is that there are differing views on what the evaluation. The solution is simple, if there is any ambiguity then use brackets.

Doh!!!

#include <stdio.h>

int main()
{
    int x=3;
    int y=4;
    int z=4;

    printf(" z >=  y  >= x  %d \n",  z >=  y  >= x);
    printf("(z >=  y) >= x  %d \n", (z >=  y) >= x);    
    printf(" z >= (y  >= x) %d \n",  z >= (y  >= x));

    return 0;
}

Results:

z >=  y  >= x  0
(z >=  y) >= x  0
 z >= (y  >= x) 1

So obviously left to right...

Doh!!!

#include <stdio.h>

int main()
{
    int x=3;
    int y=4;
    int z=4;

    printf(" z >=  y  >= x  %d \n",  z >=  y  >= x);
    printf("(z >=  y) >= x  %d \n", (z >=  y) >= x);    
    printf(" z >= (y  >= x) %d \n",  z >= (y  >= x));

    return 0;
}

Results:

z >=  y  >= x  0
(z >=  y) >= x  0
 z >= (y  >= x) 1

So obviously left to right...

Then what about the evaluation of the printf statement like

int i=3;
printf("%d  %d",i++ + ++i,i);

and

int i=3;
printf("%d   %d",i++ + ++i,i++);

Is it again from left to right?

Its always from left to right even if there are parentheses in the expression. The example you posted displays undefined behavior, meaning there is no consistent result among compilers.

Its always from left to right even if there are parentheses in the expression. The example you posted displays undefined behavior, meaning there is no consistent result among compilers.

then if i am given a question in the interview or else is it the same i have to say?
and all the increment or decrement operators show undefined behavoiur?

In the example posted, yes they are all undefined behavior. Here is more in-depth explanation.

In the example posted, yes they are all undefined behavior. Here is more in-depth explanation.

int main(int argc, char ** argv)
{

   int i = 0;
   i = i++ + ++i;
   printf("%d\n", i); // 3 
  // this one will be 4 as i++ + ++i (1+3)

   i = 1;
   i = (i++);
   printf("%d\n", i); // 2 Should be 1, no ?

   volatile int u = 0;
   u = u++ + ++u;
   printf("%d\n", u); // 1

   u = 1;
   u = (u++);
   printf("%d\n", u); // 2 Should also be one, no ?
  //how this one could be 1?
  //(u++) post incremented and as is it in parantheses we can say that the value of u will become 2

   register int v = 0;
   v = v++ + ++v;
   printf("%d\n", v); // 3 (Should be the same as u ?)
}

isnt?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.