Learn how to convert an expression from Infix to Prefix using Stack in C Programming. This code for infix to prefix in c uses two arrays to store infix and prefix expression and a stack for conversion from infix to prefix expression.
Infix To Prefix Conversion Example
Infix String: X+Y/Z-M*N+O
Prefix String: +-+X/YZ*MNO
C Program For Infix To Prefix Conversion using Stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> int pop(); int precedence(char symbol); int isEmpty(); void infix_to_prefix(); int checker(char symbol); void push(long int symbol); char prefix_string[20], infix_string[20], postfix_string[20]; int top; long int stack[20]; int main() { int count, length; char temp; top = -1; printf("\nEnter an Expression in Infix format:\t"); scanf("%[^\n]s", infix_string); infix_to_prefix(); printf("\nExpression in Postfix Format: \t%s\n", postfix_string); length = strlen(postfix_string) - 1; strncpy(prefix_string, postfix_string, 20); for(count = 0; count < length; count++, length--) { temp = prefix_string[count]; prefix_string[count] = prefix_string[length]; prefix_string[length] = temp; } printf("\nExpression in Prefix Format: \t%s\n", prefix_string); return 0; } void infix_to_prefix() { unsigned int count, temp = 0; char next; char symbol; for(count = 0; count < strlen(infix_string); count++) { symbol = infix_string[count]; if(!checker(symbol)) { switch(symbol) { case '(': push(symbol); break; case ')': while((next = pop()) != '(') { postfix_string[temp++] = next; } break; case '+': case '-': case '*': case '/': case '%': case '^': while(!isEmpty() && precedence(stack[top]) >= precedence(symbol)) postfix_string[temp++] = pop(); push(symbol); break; default: postfix_string[temp++] = symbol; } } } while(!isEmpty()) { postfix_string[temp++] = pop(); } postfix_string[temp] = '\0'; } int precedence(char symbol) { switch(symbol) { case '(': return 0; case '+': case '-': return 1; case '*': case '/': case '%': return 2; case '^': return 3; default: return 0; } } int checker(char symbol) { if(symbol == '\t' || symbol == ' ') { return 1; } else { return 0; } } void push(long int symbol) { if(top > 20) { printf("Stack Overflow\n"); exit(1); } top = top + 1; stack[top] = symbol; } int isEmpty() { if(top == -1) { return 1; } else { return 0; } } int pop() { if(isEmpty()) { printf("Stack is Empty\n"); exit(1); } return(stack[top--]); } |
Output

In case you get any compilation errors or any doubts in this C Program For Conversion of Infix Expression to Prefix Expression, let us know about it in the Comment Section below.
This is funny. I searched for infix to prefix conversion everywhere. Didnt get any working program. But, your program works. Thanks!
Prefix means operators before the operand and infix means operators within the operands, right?
Amazing for for infix to prefix converting string. Thanks a lot.
amazing way to teach.
I copied your code in DEV even I tried many other codes of conversion but same problem appears on black screen when I give my expression and enters, screen disappears, its happening almost in every code