tags:

views:

96

answers:

3

Would anyone be kind enough to explain what infix, postfix and prefix notation is in regards to the C programming language?

Thanks, ~Daniel

+6  A: 

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++;
Reed Copsey
Good link... +1
Platinum Azure
+4  A: 

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
Johannes Schaub - litb
That function call `f()` is clearly prefix, don't you think? The operator `f` is being applied to the parameters in the parentheses.
Platinum Azure
ha! maybe.. but if `f` is a token - i.e. user defined - then surely `()`, as a part of the language, must be the operator acting on it?
Sanjay Manohar
@Platinum it's called a postfix expression, because `f` can be a postfix-expression in turn and the `()` operator follows it, which makes the whole thing group left to right: `a[i]()`. It's not a clear cut though. In C99 `(int){0}` is a postfix expression too (compound literal), but surely there is no clear "postfix" in there :)
Johannes Schaub - litb
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:

  1. Primary expressions (variable names, literals, parenthesized expressions)
  2. Array subscript expressions (a[i])
  3. Component selection (a->b, d.c)
  4. Function calls (foo(a,b))
  5. Post increment and decrement expressions (a++, b--)
  6. Compount literals (C99 only - (int []) {1, 2, 3})

All postfix expressions have higher precedence than unary or binary expressions.

Unary expressions include the following:

  1. Postfix expressions
  2. Cast expressions ((int) foo)
  3. Sizeof expressions (sizeof foo, sizeof (int))
  4. Unary minus (-5)
  5. Unary plus (+1)
  6. Logical negation (!expr)
  7. Bitwise negation (~byte)
  8. Address expression (&foo)
  9. Indirection expression (*ptr)
  10. 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:

  1. Multiplicative expressions (a * b, c / d)
  2. Additive expressions (a + b, c - d)
  3. Shift expressions (a << b, val >> 2)
  4. Relational expressions (a < b, c >= d)
  5. Equality expressions (a == b, c != d)
  6. 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.

John Bode