Question regarding stacks

This is a discussion on Question regarding stacks within the C Programming forums, part of the General Programming Boards category; I need a little explanation on one of the lines in this program. What does (what-'0') do? Thanks in advance. ...

  1. #1
    Registered User glanzvoll's Avatar
    Join Date
    Feb 2010
    Posts
    5

    Question regarding stacks

    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);
    }
    Last edited by glanzvoll; 04-28-2010 at 03:32 AM.

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    It's trickery.

    what is type char. char's are stored as numbers. ASCII codes for characters '0', '1' and '2' are 48, 49 and 50, respectively. Subtracting 48 from 49 or 50 results in either 1 or 2, respectively. 1 and 2 are, respectively, values for PUSH and POP constants.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  2. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  3. N00B question about stacks
    By LobbDogg in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2003, 01:26 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. question about stacks (and vectors too for that matter)
    By Silvercord in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2003, 12:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21