C Expression Conversion

Intermediate
6 min read
Updated: Nov 2026
1 example

Infix to Postfix

People write infix (A+B*C); computers evaluate postfix (ABC*+) more easily because it needs no parentheses or precedence rules. The shunting-yard idea: operands go straight to the output; operators wait on a stack and pop when an operator of equal or higher precedence arrives; ( is pushed, and ) pops until (.

Remember
operand      -> output
'('          -> push
')'          -> pop to output until '('
operator     -> pop while prec(top) >= prec(op); push op
end          -> pop everything

Cheat Sheet

Keep these facts handy while you work with Expression Conversion in C.

TopicDetail
Precedence* / over + -
AssociativityLeft to right pops equals
ParenthesesPush (, pop at )
TimeO(n)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Expression Evaluation.

Example 1 — Convert three expressions

Single-letter operands, four operators, parentheses.

C
#include <ctype.h>
#include <stdio.h>

static int prec(char c) {
    if (c == '+' || c == '-') return 1;
    if (c == '*' || c == '/') return 2;
    return 0;
}

static void to_postfix(const char *in, char *out) {
    char st[64];
    int top = -1, k = 0;
    for (; *in; in++) {
        char ch = *in;
        if (isalnum((unsigned char)ch)) out[k++] = ch;
        else if (ch == '(') st[++top] = ch;
        else if (ch == ')') {
            while (top >= 0 && st[top] != '(') out[k++] = st[top--];
            top--;
        } else {
            while (top >= 0 && prec(st[top]) >= prec(ch)) out[k++] = st[top--];
            st[++top] = ch;
        }
    }
    while (top >= 0) out[k++] = st[top--];
    out[k] = '\0';
}

int main(void) {
    const char *tests[] = {"A+B*C", "(A+B)*C", "A*B+C/D"};
    char out[64];
    for (int i = 0; i < 3; i++) {
        to_postfix(tests[i], out);
        printf("%s -> %s\n", tests[i], out);
    }
    return 0;
}

How It Works

  1. In A+B*C, + waits because * has higher precedence.
  2. Parentheses force A+B out first in the second case.
  3. In A*B+C/D, * is popped when + arrives, but / does not pop +.

Edge Cases & Pitfalls

Check these before relying on Expression Conversion in real programs.

prec

Pop condition off by one

Use >= for left-associative operators. Using > gives wrong results for A-B-C.

paren

Unbalanced parentheses

Validate parentheses first, or handle a missing (.

Key Takeaways

  • Operands go straight out.
  • Pop on equal or higher precedence.
  • Parentheses bracket sub-expressions.

One line: People write infix (A+B*C); computers evaluate postfix (ABC*+) more easily because it needs no parentheses or precedence rules.

Frequently Asked Questions

An expression format where operators follow their operands, such as 2 3 + instead of 2 + 3. It needs no parentheses.
The shunting-yard algorithm, using a stack of pending operators.
Continue with "C Expression Evaluation" next. Evaluate postfix expressions using an operand stack. The C Stacks hub lists every topic in order.

Next: Expression Evaluation

Evaluate postfix expressions using an operand stack.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful