Would anyone be kind enough to explain what infix, post开发者_开发技巧fix and prefix notation is in regards to the C programming language?
Here is a good discussion of the three terms, and how they apply.
The C language uses infix notation nearly everywhere. For example, you'd do:
x = 4 + 2;
However, there are a couple of operations that use prefix notation, such as negation:
x = -y; // "-" is using prefix notation
Postfix is used for operations such as incrementation (++):
x = y++;
Some examples for each:
Infix:
a + b
a * b
Postfix:
a++
f()
a[i]
Prefix (also called "unary" in C and C++):
++a
&a
-a
The C language definition isn't quite so clear-cut about infix vs. prefix vs. postfix; the terms "prefix" and "infix" don't even appear in the language definition as such. Instead, you have binary operator expressions (sort-of infix) and unary expressions (sort-of prefix). Also, the groupings of expressions into postfix, unary, and binary categories has more to do with which expressions should have higher precedence than others, rather than where the operators appear in the expression.
For example, the &&
and ||
operators look like they should be grouped with the binary operator expressions, but they're not; they occupy their own space in the language grammar and they have their own unique semantics. The component selection operators .
and ->
are grouped with the postfix operators, even though they appear between two expressions, because component selection should always have higher precedence than, say, address indirection (i.e., &a.b
should always be parsed as &(a.b)
, not (&a).b
) and also because the right-hand operand must always be an identifer.
Postfix expressions include the following:
- Primary expressions (variable names, literals, parenthesized expressions)
- Array subscript expressions (
a[i]
) - Component selection (
a->b
,d.c
) - Function calls (
foo(a,b)
) - Post increment and decrement expressions (
a++
,b--
) - Compount literals (C99 only -
(int []) {1, 2, 3}
)
All postfix expressions have higher precedence than unary or binary expressions.
Unary expressions include the following:
- Postfix expressions
- Cast expressions (
(int) foo
) - Sizeof expressions (
sizeof foo
,sizeof (int)
) - Unary minus (
-5
) - Unary plus (
+1
) - Logical negation (
!expr
) - Bitwise negation (
~byte
) - Address expression (
&foo
) - Indirection expression (
*ptr
) - Pre increment and decrement (
++foo
,--bar
)
Unary expressions have higher precedence than binary expressions, but lower precedence than postfix expressions.
Binary expressions include the following:
- Multiplicative expressions (
a * b
,c / d
) - Additive expressions (
a + b
,c - d
) - Shift expressions (
a << b
,val >> 2
) - Relational expressions (
a < b
,c >= d
) - Equality expressions (
a == b
,c != d
) - Bitwise expressions (
a & b
,c | d
,e ^ f
)
Binary expressions have lower precedence than both postfix and unary expressions.
In addition to these groupings, you also have the logical operator expressions (&&
and ||
), conditional expressions (a ? b : c
), which have a lower precedence than binary expressions, assignment expressions (a = b
, c += d
), sequential expressions (a, b, c
), and constant expressions (int a[42]
, case 5:
), which are different from the literals classed with postfix expressions.
精彩评论