I need a little explanation on one of the lines in this program. What does (what-'0') do? Thanks in advance.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define PUSH 1
#define POP 2
struct LLNode{
char key;
struct LLNode *next;
};
struct LLNode *initLLNode(char val);
char empty(struct LLNode *head, struct LLNode *tail);
void push(char val, struct LLNode *head);
char pop(struct LLNode *curr);
int main(void){
char value, what;
struct LLNode *head;
struct LLNode *tail;
head=initLLNode(NULL);
tail=initLLNode(NULL);
head->next=tail;
printf("options:\n\t[%d]push\n\t[%d]pop\n\t[0]end\n",
PUSH,POP);
do{
printf("\n\t\t\t\t[%d][%d][0]>>",PUSH,POP);
while(isspace(what=getchar()));
switch(what-'0'){
case PUSH :
printf("\t\t\t\tPush what? ");
while (isspace(value=getchar()));
push(value,head);
printf("'%c' pushed\n",value);
break;
case POP :
if(empty(head,tail))printf("Stack Empty\n");
else{
value = pop(head);
printf("'%c' popped\n", value);
}
break;
default: break;
}
}while(what-'0');
printf("program finished\n");
return(0);
}
struct LLNode *initLLNode(char val){
struct LLNode *temp;
temp=(struct LLNode *)malloc(sizeof(struct LLNode));
temp->key=val;
temp->next=NULL;
return(temp);
}
char empty(struct LLNode *head, struct LLNode *tail){
/* returns 1 if empty, 0 otherwise */
if(head->next==tail)return(1);
else return(0);
}
void push(char val, struct LLNode *head){
struct LLNode *temp;
temp=initLLNode(val);
temp->next=head->next;
head->next=temp;
return;
}
char pop(struct LLNode *curr){
struct LLNode *temp;
char val;
val=curr->next->key;
temp=curr->next;
curr->next=curr->next->next;
free(temp);
return(val);
}