Logical comparison operators without control statements

sanjeev mk

Consider this:

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

The output of the above code is:

1 1 1 1 1 1 1 1

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

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

Initially, I thought there'd be no change in the output because the results of the comparisons using && , || are not being evaluated by a control statement like if or while. That is, they're being discarded. However, The output of this code is:

1 1 1 1 1

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

My question: What does this line do

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

Definitely, something has changed. I've never contemplated using comparison operators like && , || without control statements like if, while to evaluate the result of the comparison and accordingly take some action. I thought, without these control constructs, the comparison operators are meaningless; they'd simply return 1 or 0 without anything being done about it.

Clearly, I'm mistaken, and am totally unaware of this usage of the operators.

Ry-

fork() returns the PID for a child process if it’s returning in the parent, or 0 if it’s returning in the child. Logical operators short-circuit in C, so a && b doesn’t evaluate b if a is 0, and a || b doesn’t evaluate b if a is not 0.

So you fork once. Two processes. The right side of the && only gets evaluated in the child. Three processes. The right side of the || gets evaluated in the same child again. Four processes. But wait! && binds tighter than ||, so the parent process also forks again, for a total of five processes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use of logical and comparison operators in conditions (javascript)

From Dev

User written formats and comparison/logical operators in SAS

From Dev

Number comparison without using if statements

From Dev

Number comparison without using if statements

From Dev

Rounding integer division without logical operators

From Dev

Determine triangle type WITHOUT logical operators JAVA

From Dev

If, else if, else statements and logical operators in R and creating functions

From Dev

Change comparison operators without large conditional block

From Dev

Logical AND (&&) and OR (||) operators

From Dev

Logical AND (&&) and OR (||) operators

From Dev

Logical operators

From Dev

Logical Operators and increment operators

From Dev

How to print 2 characters in C without using logical operators?

From Dev

Getting more control over the rich comparison operators in python2

From Dev

Control Flow of switch statements without breaks

From Dev

Determine a menu choice without using logical operators, relational operators, or selection constructs

From Dev

How can I combine the $not Logical Query Operator in conjunction with other Comparison Query Operators to get a more specific yield?

From Dev

Rewriting a piece of C code without conditional statements or operators?

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

Rewriting a piece of C code without conditional statements or operators?

From Dev

How can I write a conditional without any conditional statements or operators?

From Dev

Creating a filter view in Jspresso containing a date picker without comparison operators

From Dev

Creating a filter view in Jspresso containing a date picker without comparison operators

From Dev

What is the type of the logical operators?

From Dev

Logical operators with three lists

From Dev

Logical Operators in Tweepy Filter

From Dev

Precedence of the shell logical operators &&, ||

From Dev

Combining logical operators in Ruby

From Dev

Are logical operators in assertions acceptable?

Related Related

  1. 1

    Use of logical and comparison operators in conditions (javascript)

  2. 2

    User written formats and comparison/logical operators in SAS

  3. 3

    Number comparison without using if statements

  4. 4

    Number comparison without using if statements

  5. 5

    Rounding integer division without logical operators

  6. 6

    Determine triangle type WITHOUT logical operators JAVA

  7. 7

    If, else if, else statements and logical operators in R and creating functions

  8. 8

    Change comparison operators without large conditional block

  9. 9

    Logical AND (&&) and OR (||) operators

  10. 10

    Logical AND (&&) and OR (||) operators

  11. 11

    Logical operators

  12. 12

    Logical Operators and increment operators

  13. 13

    How to print 2 characters in C without using logical operators?

  14. 14

    Getting more control over the rich comparison operators in python2

  15. 15

    Control Flow of switch statements without breaks

  16. 16

    Determine a menu choice without using logical operators, relational operators, or selection constructs

  17. 17

    How can I combine the $not Logical Query Operator in conjunction with other Comparison Query Operators to get a more specific yield?

  18. 18

    Rewriting a piece of C code without conditional statements or operators?

  19. 19

    How can I write a conditional without any conditional statements or operators?

  20. 20

    Rewriting a piece of C code without conditional statements or operators?

  21. 21

    How can I write a conditional without any conditional statements or operators?

  22. 22

    Creating a filter view in Jspresso containing a date picker without comparison operators

  23. 23

    Creating a filter view in Jspresso containing a date picker without comparison operators

  24. 24

    What is the type of the logical operators?

  25. 25

    Logical operators with three lists

  26. 26

    Logical Operators in Tweepy Filter

  27. 27

    Precedence of the shell logical operators &&, ||

  28. 28

    Combining logical operators in Ruby

  29. 29

    Are logical operators in assertions acceptable?

HotTag

Archive