Code for a Calculator

static int n[MAX_NUMBERS_TO_BE_ENTERED];	/* array is initialized to zero */
static int i; /* initialized to zero */
static char op;
static int result;
static BOOL valid_result = FALSE;
  
void OnCharTyped(char c)
{
	if(c>='0' && c<='9') {
		if(n<1) n[i] = c - '0';
		else n[i] = n[i]*10 + c - '0';
		return;
	}
	if(c==CODE_FOR_ENTER) {
		i++;
		Redraw();
		return;
	}
	if(c=='+' || c=='-' || c=='*' || c=='/') {
		op = c;
		return;
	}
	if(c=='=') {
		if(n>0) {
			if(op=='+') {
				result = n[i]+n[i-1];
				valid_result = TRUE;
				ReDraw();
				return;
			}
			/* code for other operations */
	}
	/* code to deal with other keys */
}

This is not good code but it illustrates the concept. The first step to improve the code will be to declare an object (structure) and make all the static variables and the OnCharTyped() function members of that object. Messages from the system can be delivered to a particular object so this solution avoids the use of static variables.