1

I am learning C. I came across the following program -

#include <stdio.h>

int main() {
  int var1 = 2, var2 = 6;
  var2 = var2 || var1++ && printf("Computer World");
  printf("%d %d\n", var1, var2);
  return 0;
}

After compilation with gcc 4.4.5 on Ubuntu 10.10, I'm getting the output as -

2 1

I understand how 'var2' is set to 1.

Even thought there is a increment operator on 'var1', why it is not incremented when we see the console output?

ganesh
  • 23
  • 1
  • 6
  • 1
    That is not the output of that program. – Jonathon Reinhart Jan 03 '14 at 07:39
  • @JonathonReinhart he probably meant `2 1` and mistakenly added a prompt before that. But *I* do not understand how `var2` is set to 1... – icedwater Jan 03 '14 at 08:02
  • @icedwater You are right... I'll not add that next time ... Seems to cause confusion. – ganesh Jan 03 '14 at 08:05
  • Yes, because $ marks variables in bash, too. e.g. defining `var2=50` in a bash script will return `50` when you `echo $var2`. – icedwater Jan 03 '14 at 08:06
  • Who will write a code like this var2 = var2 || var1++ && printf("Computer World");, anyways var2 is positive hence no operation at RHS and assigns true which 1 to var2 – asio_guy Jan 03 '14 at 09:36

2 Answers2

3
var2 || var1++ && printf("Computer World");

is a logic operation so if the var2 is true (var2 is not equal to zero) then the second logic operation var1++ && printf("Computer World"); will not be executed (It's called a short-circuit operation) . So that's why the var1 is not incremented

Try to inverse your logic operation in this way and you will get the var1 incremented:

var2 = var1++ && printf("Computer World") || var2;
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • It's called a [short-circuit operator](https://en.wikipedia.org/wiki/Short-circuit_evaluation) – Njol Jan 03 '14 at 07:42
  • the second logic operation **may** not get executed. It's unspecified. – Dariusz Jan 03 '14 at 07:42
  • @Njol thank you for the help I updated my answer with your remark – MOHAMED Jan 03 '14 at 07:45
  • 1
    @Dariusz Citation please? – Jonathon Reinhart Jan 03 '14 at 07:46
  • 1
    @Dariusz I think that actually it is specified for both `c` and `c++`. This is why compilers cannot naturally paralellize complex logic statements. EDIT: http://stackoverflow.com/questions/628526/is-short-circuiting-boolean-operators-mandated-in-c-c-and-evaluation-order I guess one needs to be careful. But in `c` and generally `c++` you can depend on it. – luk32 Jan 03 '14 at 07:47
  • Thank you all for telling about this pitfall. – ganesh Jan 03 '14 at 08:00
  • Thanks, I didn't know that logical `&&` and `||` are evaluated left-to-right. I stand corrected. – Dariusz Jan 03 '14 at 08:03
0

In C, || is a shortcut operator, which means in an expression such as exp1 || exp2, if the truth value of this expression can be determined by evaluating exp1, exp2 would not be evaluated.

For example, in your case, the result of evaluating var2 is 6, which is true in C, so other part of the expression, which is var1++ && printf("Computer World"), would not be evaluated.

You can see Shortcut Operators for further details.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47