Exforsys
+ Reply to Thread
Results 1 to 2 of 2

Which has higher priority- Somebody throw highlight on this?

This is a discussion on Which has higher priority- Somebody throw highlight on this? within the C and C++ forums, part of the Programming Talk category; Hi All, I have a code as below main () { int x,y,z; x=1; y=-1; z=2; z=++x&&++y||++z; printf("x=%d y=%d z=%d",x,y,z); ...

  1. #1
    Allan is offline Member Array
    Join Date
    May 2006
    Answers
    40

    Which has higher priority- Somebody throw highlight on this?

    Hi All,
    I have a code as below
    main ()
    {
    int x,y,z;
    x=1;
    y=-1;
    z=2;
    z=++x&&++y||++z;
    printf("x=%d y=%d z=%d",x,y,z);
    }
    In the above code I could not figure out which takes higher precedence whether && or ||. Only if I could get this idea clear my output would be correct. So somebody clarify this.

    Regards,
    Allan


  2. #2
    Ralph is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    114
    The logical AND denoted as && takes precedence over logical OR denoted as ||.For both the order of operations would be from left to right.

    The process of output would be as follows:
    ++x is equal to 2
    ++y is equal to 0
    ++z is equal to 3
    So
    ++x&&++y is equal to 2&&0 which is equal to 0

    and so 0||++z is equal to 0||3 which is equal to true.

    Allan I assume your query is clarified.


Latest Article

Network Security Risk Assessment and Measurement

Read More...