1
int main()
{
    int var1=4, var2=6;
    var2=var2||var1++&&printf("computer world");
    printf("%d%d",var1,var2); 
    getch();
}

The printed answer is 41.

Question:
Here it is not printing the 'computer world'. According to order of priority we have to calculate the && logical operator but the behaviour here is not like that and the value of var1 should increment after that statement but doesn't. My expected answer is computer world51.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
vinod kumar
  • 175
  • 2
  • 3
  • 6
  • 1
    This is a duplicate of at least one recent question, and possibly several older ones. It is an issue of precedence versus order of execution. – Jonathan Leffler Jul 25 '13 at 06:00
  • you just copy paste the source code in google. You can find multiple sites having same question. – Pandiyan Cool Jul 25 '13 at 06:04
  • 1
    Possible duplicate of [Problem with operator precedence](http://stackoverflow.com/questions/7212482/problem-with-operator-precedence). Another one was [Why lower-precedence operator executes first](http://stackoverflow.com/questions/12152143/why-lower-precedence-operator-executes-first). – Jonathan Leffler Jul 25 '13 at 06:04

6 Answers6

6

It's called "precedence", not "priority". And precedence is not the same as order of evaluation.

Since the || and && operators short-circuit, and var2 is considered true (since 6 is non-zero), neither the post-increment operation nor the function call is evaluated.

1

The || operator checks if the first value is true. If it is true then it won't evaluate the second part of the logical expression and hence the printf statement has not output the text .This is called as short circuit operation.

The below code gives the expected result

#include<stdio.h>

int main()
{
     int  var1=4,var2=0;
     var2=var2||var1++&&printf("computer world");
     printf("%d%d",var1,var2); 
     getch(); //remove getch if you are working on gcc 
}
Santhosh Pai
  • 2,535
  • 8
  • 28
  • 49
  • 2
    It also doesn't execute the `var1++`. – Jonathan Leffler Jul 25 '13 at 06:01
  • Sir @JonathanLeffler i made the above changes and it is working as expected. – Santhosh Pai Jul 25 '13 at 06:04
  • 1
    I'm not sure if I should ask what the relevant 'as expected' behaviour is. Your code fragment should be printing `computer world51` since `var2` is 0 (false) so the post-increment happens, but the value returned is 4 (the before increment value), so the `&&` condition needs to evaluate the function, which outputs `computer world` and returns 14, the number of characters printed. So, the overall expression evaluates to 1 (false), and `var2` is assigned 1, leading to the values `var1==5` and `var2==1`. There should be a newline at the end of the second `printf()` format to get sane behaviour. – Jonathan Leffler Jul 25 '13 at 06:18
  • That's a very good explanation Mr @JonathanLeffler +1 for that . The person who asked the question wanted the output as Computer World 51 so i edited the code to get the same by hardcoding the value of var2 as 0. – Santhosh Pai Jul 25 '13 at 06:26
1

var2||var1++&&printf("computer world") will be evaluated to :

var2||(  var1++&&printf("computer world")   )

so since var2 is not zero, all the part after || will be ignored.

so the result is true. which is 1 and it will be assigned to var2.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vijay
  • 65,327
  • 90
  • 227
  • 319
0

The || is a short circuit operator if the first operand evaluates to true it doesn't execute further.

So var2 becomes 1 ( Since var2 is initially 6 and the operation evaluates to true) and no increment happens so var1 remains 4.

0decimal0
  • 3,884
  • 2
  • 24
  • 39
0

|| is short-cut, so in the statement:

var2=var2||var1++&&printf("computer world");

Since var has a value of 6, which evaluates as true, the latter expression var1++&&printf("computer world"); will not be executed.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

for fun:

#include"stdio.h"                                                                                                                                                          
   main()
   {
        int  var1=4,var2=6;
        var2=printf("step1")&&var2||printf("step2")||var1++&&printf("step3");
        printf("%d%d\n",var1,var2); 
        var2=printf("step1")&&var2&&printf("step2")||var1++&&printf("step3");
        printf("%d%d\n",var1,var2); 
        var2=printf("step1")&&var2&&printf("step2")&&var1++&&printf("step3");
       printf("%d%d\n",var1,var2);
      // getch();
  }

Result is :

step141

step1step241

step1step2step351

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31