From what I read in a few books, a static variable retains its value(s) throughout the lifetime of a program.
In this stack program I've been trying to fix up, the static var doesn't seem to change or hold its contents after different function calls. I can't figure out what's wrong :cry:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 100
void push(int);
int pop();
static int s[LEN];
static int N;
int main(void)
{
char input[LEN];
int i = 0, size;
puts("Enter a postfix expression");
scanf("%s", input);
size = strlen(input);
while (i < size)
{
if (input[i] == '+')
push(pop() + pop());
if (input[i] == '*')
push(pop() * pop());
if ((input[i] >= '0') && (input[i] <= '9'))
push(0);
while ((input[i] >= '0') && (input[i] <= '9'))
push(10*pop() + (input[i++]-'0'));
i++;
}
printf("Final value is %d\n", pop());
return 0;
}
void push(int item)
{ s[N++] = item; }
int pop()
{ return s[--N]; }