Take the 2-minute tour ×

I’m taking a course in C and we have to make a program for the classic Postfix evaluation problem. Now, I’ve already completed this problem in java, so I know that we have to use a stack to push the numbers into, then pop them when we get an operator, I think I’m fine with all of that stuff. The problem I have been having is scanning the postfix expression in C. In java it was easier because you could use charAt and you could use the parseInt command. However, I’m not aware of any similar commands in C. So could anyone explain a method to read each value from a string in the form:

4 9 * 0 - =

Where the equals is the signal of the end of the input.

Any help would be greatly appreciated and thank you in advance :)

share|improve this question
possible duplicate of Postfix evaulation using stacks and C –  Jens Erat May 28 '13 at 16:31
add comment

3 Answers

Let's suppose you input is in an array of characters.

char input[] = "4 9 * 0 - =";

you can access individual characters by accessing each individual array element

if (input[4] == '*') /* deal with star */;

or you can use pointer arithmetic and parse from a different point in the input (remember to #include <stdio.h> for the prototype for `sscanf´)

if (sscanf(input + 2, "%d", &number) != 1) /* deal with error */;

Or, as suggested by Chris Lutz in a comment, use strtol (after the proper #include <stdlib.h>)

number = strtol(input + 2, &next, 10);
/* don't forget to check for errors! */
/* `next` now points to the character after the `long` at position 2 in the array */
share|improve this answer
Rather use strtol than sscanf here. strtol will help advance the string. –  Chris Lutz Nov 12 '11 at 17:44
+1 Chris: answer edited with the addition of the strtol option. Thanks. –  pmg Nov 12 '11 at 17:52
add comment

C strings are arrays of chars: char[] or char*. you can use a for loop to iterate it and get each characher by it's index:

for (int i = 0; i < strlen(yourString); i++)
    char ch = yourString[i];
    // ...

Also there is a function, strtok() that might be helpful here for tokenizing the string:

#include <string.h>
#define NULL (void*)0

char yourString[] = "4 9 * 0 - =";
char delimiters[] = " "; // could be " +*/-=" depending on your implementation
char *token = NULL;
token = strtok(yourString, delimiters);
while(token != NULL)
    printf("current token is: %s\n", token);
    // do what ever you want with the token
    token = strtok(NULL, delimiters); // next token
share|improve this answer
add comment

You can also know with sscanf how many items have been read (the counter of well read data items is the result of sscanf) and what is the relative position (using the %n format specifier).

so you could also code

int pos = 0;
int endpos = 0;
int val = 0;
if (sscanf(input + pos, "%d %n", &val, &endpos) >= 1) {
  // val has been read as an integer, handle it
  stack[top++] = val;
  pos += endpos; // skip to next token in input

There are many more ways of doing that. You might want to read about lexers and parsers, e.g. with flex and bison, or antlr, etc.

share|improve this answer
add comment

Your Answer


