Logical comparison operators without control operators

Consider this:

    main()
    {
      int i = 1;
      fork(); fork(); fork();
      printf("%d ",i);
    }

The output of the above code:

1 1 1 1 1 1 1 1

That is, at the end there are 8 (2 ^ 3) processes reaching the line printf(). Now consider the following code:

    main()
    {
      int i = 1;
      fork() && fork() || fork();
      printf("%d ",i);
    }

At first, I thought that the result would not change, because the results of comparisons using &&are ||not evaluated by a control operator such as ifor while. That is, they are discarded. However, the output of this code is:

1 1 1 1 1

Meaning: at the end there are 5 processes that reach the line printf().

My question is: what does this line do

 fork() && fork()||fork();

, - . , &&, || if, while , , . , ; 1 0, .

, .

+3
2

fork() PID , , 0, . C, a && b b, a 0, a || b b, a 0.

, . . && . . || . . ! && , ||, , .

+3

, :

exp1 && exp2 || exp3;

- . , exp1 (0), exp2 , exp1 && exp2 . exp1 && exp2 ( 0), exp3 , .

+1

All Articles