{"id":2249,"date":"2016-12-17T00:01:44","date_gmt":"2016-12-17T08:01:44","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2249"},"modified":"2016-12-10T09:23:26","modified_gmt":"2016-12-10T17:23:26","slug":"behold-the-stack-part-v","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2249","title":{"rendered":"Behold the Stack, Part V"},"content":{"rendered":"<p>Pulling in all the stack items mentioned in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2243\">last week&#8217;s Lesson<\/a>, I present to you a C program that emulates stack storage. I&#8217;m certain this presentation will be one of the highlights of your programming career.<br \/>\n<!--more--><br \/>\nI&#8217;m joshing, of course. Many programming concepts are good to know, even if you never use them. And my advice is never to force anything just to meet some artificial standard. For example, I&#8217;ve seen a lot of object oriented code that could have been written in a procedural manner, but I digress.<\/p>\n<p>The following code contains stack emulation elements. It prompts the user for integer input, up to 16 values. Each value is pushed onto stack storage. After typing the values, each is popped from the stack:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid push(int v);\r\nint pop(void);\r\n\r\n#define STACK_SIZE 16\r\n\r\nint stack[STACK_SIZE];\r\nint *sp;\r\n\r\nint main()\r\n{\r\n    int s,a;\r\n\r\n    sp = stack;     <span class=\"comments\">\/* initialize the stack pointer *\/<\/span>\r\n    s = 0;\r\n\r\n    while(s &lt; STACK_SIZE)\r\n    {\r\n        printf(\"Enter a value to push on the stack, 0 to end: \");\r\n        scanf(\"%d\",&a);\r\n        if(a == 0) break;\r\n        push(a);\r\n        s++;\r\n    }\r\n\r\n    while(s)\r\n    {\r\n        printf(\"Popping %d from the stack\\n\",pop());\r\n        s--;\r\n    }\r\n\r\n    return(0);\r\n}\r\n\r\nvoid push(int v)\r\n{\r\n    if(sp == stack+STACK_SIZE)\r\n    {\r\n        puts(\"Stack Overflow\");\r\n    }\r\n    else\r\n    {\r\n        sp++;\r\n        *sp = v;\r\n    }\r\n}\r\n\r\nint pop(void)\r\n{\r\n    if( sp == stack)\r\n    {\r\n        puts(\"Stack Empty\");\r\n        return(0);\r\n    }\r\n    else\r\n    {\r\n        return(*sp--);\r\n    }\r\n}<\/pre>\n<p>A <em>while<\/em> loop at Line 18 processes input. Variable <code>s<\/code> tracks integers in variable <code>a<\/code> that are pushed onto the stack<\/p>\n<p>Line 22 checks for early termination when 0 is input. Otherwise, the <em>push()<\/em> statement at Line 23 sends the value to the stack.<\/p>\n<p>A second <em>while<\/em> loop at Line 27 pops values from the stack.<\/p>\n<p>Here is sample output:<\/p>\n<pre><code>Enter a value to push on the stack, 0 to end: 100\r\nEnter a value to push on the stack, 0 to end: 200\r\nEnter a value to push on the stack, 0 to end: 300\r\nEnter a value to push on the stack, 0 to end: 0\r\nPopping 300 from the stack\r\nPopping 200 from the stack\r\nPopping 100 from the stack<\/code><\/pre>\n<p>In my programming career, I&#8217;ve never needed to emulate a stack in any of my C code. I might have done so and not recognized it as such. Then again, in Assembly, using the stack is vital and something you take care to monitor. In fact, recursion freaks me out in C because I&#8217;m aware of how much this technique uses the stack and the potential for a stack overflow. That&#8217;s always possible, of course, even when you emulate stack storage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an actual C program that emulates stack storage. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2249\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2249","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2249"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2249\/revisions"}],"predecessor-version":[{"id":2258,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2249\/revisions\/2258"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}