Puzzles in C Language 1.3 logical operators and incremental Operators

Source: Internet
Author: User


Puzzle 1.3 logical operators and incremental Operators

What is the output of this program?

# Define print (INT) printf ("% d/N", INT)

Main ()


Int x, y, z;

X = 2; y = 1; Z = 0;

X = x & Y | z; print (x); (1.3.1)

Print (X |! Y & Z); (1.3.2)

X = y = 1;

Z = x ++-1; print (x); print (z); (1.3.3)

Z + =-x ++ + Y; print (x); print (z); (1.3.4) <-------

Z = x/++ X; print (z); (1.3.5)



1 (1.3.1)

1 (1.3.2)

2 (1.3.3)


3 (1.3.4)


? (1.3.5)

Confusing 1.3 logical operators and incremental Operators


Initial Value: x = 2, y = 1, Z = 0


X = x & Y | z


X = (X & Y) | z
X = (X & Y) | z)
(X = (X & Y) | z ))

Bind according to the operator priority and association rules.

(X = (true & True) | z ))

The order in which logical operators are evaluated is from left to right. If the operand of the logical operator is 0, it is interpreted as false; if it is a non-zero value, it is interpreted as true.

(X = (true | z ))

Only when both operands are true can the result of logic and (&) be true; otherwise, the result is false.

(X = (true | any value ))

If one operand is true, the result of logic or (|) is true no matter what the other operand is. Therefore, we no longer need to evaluate this expression.

(X = true)
(X = 1)


Further discussionDefine: The define statement in this program is different from the define statement in the previous program: the "print" in this program is a macro with parameters. When a macro with parameters is encountered, the C language Preprocessor will replace the form parameter in the macro definition with the actual parameter, and then replace the macro call with the macro definition body.

In this program, the "print" macro has a form parameter Int. "Print (x)" is a "print" call using the actual parameter "X. When extending the "print" call, the C language Preprocessor will first replace all "Int" in the macro definition with "X", and then call the macro "print (x) "Replace with the result string" printf ("% d/N", x )". Note that the formal parameter int does not match the "int" character in the word "printf. This is because the formal parameter in the macro definition is the identifier -- in this example, the formal parameter int only matches and replaces the identifier Int.


Initial Value: x = 1, y = 1, Z = 0


X |! Y & Z


X | (! Y) & Z
X | ((! Y) & Z)
(X | ((! Y) & Z ))

Binds operators and operands.

(True | ((! Y) & Z ))
(True | any value)
True, that is, 1

Returns the value from left to right.


Initial Values: x = 1, y = 1


Z = x ++-1


Z = (x ++)-1
Z = (x ++)-1)
(Z = (x ++)-1 ))

Binds operators and operands.

(Z = (1-1), then x = 2

(Z = 0)

The "+ +" operator on the right of the operand is the so-called "Post" incremental operator. This operator is used in the expression to complete the calculation and then incrementing.


Initial Value: x = 2, y = 1, Z = 0


Z + =-x ++ y


Z + =-(x ++) + (++ y)

The Association of The unary operator is from right to left. Therefore, the "++" operator is bound prior to the unary operator. (In fact, if you bind the "-" operator first, this expression will not work. This is because the "++" and "--" operators both require a variable (more accurately, with a "Left value") as their operands. "X" is a left value, but "-X" is not .)

Z + = (-(x ++) + (++ y)
Z + = (-(x ++) + (++ y ))
(Z + = (-(x ++) + (++ y )))


(Z + = (-2) + 2), at this time x = 3, y = 2
(Z + = 0)
(Z = 0 + 0)
(Z = 0)

Values are obtained sequentially from the inner to the outer.


MARK: the source code text of a computer program is composed of a series of marks, and the first task of compiling a program is to split those marks one by one. This is not difficult in most cases, but some character sequences may be confusing. For example, if some of the expressions in the above example do not have spaces at all -- like the following:

X ++ y

So, will the expressions without spaces be equivalent to those with spaces?

To avoid ambiguity, if a string can be interpreted as multiple operators, the C language compiler will make a choice based on the principle of "the more characters that constitute the operators, the better. According to this principle, "x ++ y" is interpreted:

X ++ y

This is no longer a legal expression.


Initial Value: x = 3, Z = 0


Z = x/++ x
Z = x/(++ X)
Z = (x/(++ X ))
(Z = (x/(++ X )))


If you want to evaluate the expression in the order from inside to outside as before, that is, increase X first, then use X as the divisor, and use X as the divisor for Division calculation. Q: What is X as the divisor? Is it 3 or 4? In another way, is the divisor the X value before increment or the X value after increment? Note that the C language does not clearly define this "side effect", but is determined by the C compiler. The lesson in this example is: if you cannot determine whether it will produce any side effects, try not to write such expressions.



1. the compiler recognizes expressions that follow the "Big Mouth" or "greedy" method, that is, to read as many meaningful characters as possible. Note that spaces can be used to separate characters.

2. Z + =-x ++ + Y: the association between the unary operator is from right to left. Therefore, the "++" operator is bound prior to the unary operator.

3 .! The priority is higher than & and the priority is higher than |, that is! --> & --> |

When & is false on the left, the right side is not calculated. Correspondingly, | if the left side is true, the right side is not calculated.

4. undefined: (Z = (x/(++ X )))

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.