6 Answers
6

you need to parse the string. there's no eval() in C (as in most static languages), so you need to either write your own parser or find some library to help.

since most easy to use parsers are for C++ and not C, i'd rather use a full embeddable language. my absolute favorite is Lua, which can be incredibly lightweight if you don't include the libraries. also, the syntax is nicer than C's, so your users might like it better.

of course, Lua is a full-blown programming language, so it might not be appropriate, or maybe it could help in other ways (to make it easier to extend your application).

Certainly the most instructive way (and possibly even the easiest, once you know how) is to learn how to write your own recursive descent parser. A parser for infix expressions in C isn't very long.

Here's one of a number of excellent blog posts by Eli Bendersky on parsing. (This one is the one that's most relevant to you, but I highly recommend all of them.) It contains source code for an infix expression parser -- admittedly in Python, not C, but the conversion should be fairly straightforward, and you'll learn a lot in the process.

One clean (possible not short) way to do it is to build a tree, like a compiler would.

For example, say you have the expression "2+3". The '+' would be the head. The '2' would be the left child and the '3' would be the right child.

Since each expression evaluates to a value, this tree can be extended for infinitely complex expressions: it just needs to be sorted in order of precedence for each operator. Low precedence operators (like '+' go at the top, while high-precedence operators (like '*') go at the bottom. You would then evaluate the expressions on the tree from the bottom up.

By easiest, by which you mean shortest, do you mean fastest to code? If so, build a string and pass it to another process via popen or similar. If you don't want an external process, embed a lua interpreter as Javier suggested. +1 to Javier as that is probably what you want.

Convert the string into an array of tokens which are the operands and operators.
Convert the infix token array to a Reverse Polish Notation array.
After the equation is in RPN, then you can pop tokens off the stack and operate on them.

Take a look at the Wikipedia article on Reverse Polish Notation. It shows how to do the conversion and the calculation.