954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C simple problem

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

}


the answer is zero ..how??

srinath1
Newbie Poster
11 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

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

shibblez
Junior Poster in Training
72 posts since Oct 2010
Reputation Points: 15
Solved Threads: 6
 

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 ???

srinath1
Newbie Poster
11 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

>>printf evaluation will be done right to left

I think you misunderstood the lecturer.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 :)

Shardendu
Newbie Poster
19 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

>>,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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

shibblez
Junior Poster in Training
72 posts since Oct 2010
Reputation Points: 15
Solved Threads: 6
 
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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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?

sharathg.satya
Posting Whiz in Training
264 posts since Oct 2010
Reputation Points: 0
Solved Threads: 5
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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?

sharathg.satya
Posting Whiz in Training
264 posts since Oct 2010
Reputation Points: 0
Solved Threads: 5
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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?

sharathg.satya
Posting Whiz in Training
264 posts since Oct 2010
Reputation Points: 0
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: