Convert an infix expression to postfix (shunting-yard).
#include <ctype.h>
static int prec(char op) { return (op == '+' || op == '-') ? 1 : (op == '*' || op == '/') ? 2 : 0; }
/* single-character operands, + - * / and parentheses; out must be large enough */
void infix_to_postfix(const char *in, char *out)
{
char st[64]; int top = 0, k = 0;
for (; *in; in++) {
char c = *in;
if (isalnum((unsigned char)c)) out[k++] = c;
else if (c == '(') st[top++] = c;
else if (c == ')') {
while (top && st[top - 1] != '(') out[k++] = st[--top];
if (top) top--; /* discard '(' */
} else if (prec(c)) {
while (top && prec(st[top - 1]) >= prec(c)) /* >= : left-associative */
out[k++] = st[--top];
st[top++] = c;
}
}
while (top) out[k++] = st[--top];
out[k] = '\0';
}
/* "a+b*(c-d)" -> "abcd-*+" */
Points: operands go straight to the output; operators wait on a stack and are popped while the stack top has higher or equal precedence (use > for right-associative operators like ^); parentheses group. Real versions handle multi-digit numbers, errors (unbalanced parentheses) and unary minus.