r/cprogramming • u/Creative-Copy-1229 • Feb 18 '26
Could you review my code
Hello everyone. I am a beginner in C. I wrote a calculator that's slightly more useful than simple "input number one, operation, number two". It accepts simple arithmetic expressions. Please can you review the code and tell me is it really bad, and what I should improve. A person said this code is bad even for beginner level, that it's a mess, so I decided I would like to see other opinions
4
Upvotes
6
u/jaynabonne Feb 18 '26
A few comments:
*infix_expr >= '0' && *infix_expr <= '9'
which is repeated in a few places.
In terms of the overall approach, I don't have the mental capacity at the moment to fully dive in and fathom the logic behind it. The code itself seems ok as code, but it might be worth putting some of it in functions just to give insight into the larger context of what's going on. (My instinct, personally, would be to add some comments to make clear what is going on, in terms of giving a "what the heck is going on in my head as I'm writing this" beyond what it's doing, but I know a lot of people have a knee-jerk reaction to them, as some sort of personal failing. It can really help someone looking at your code understand the intention behind it all, though.)
1
u/knouqs Feb 18 '26
Welcome to programming in C! Since you've asked nicely, here is some of the feedback I have regarding your code.
Please add comments to your code. Eventually you may wish to revisit this and you may wonder what your intention was. Also, it gets you in the habit of writing comments, and eventually you will be able to learn what good commentary is. How to start: Write comments in this file and then return to it in two months. See if you like the comments you wrote.
I don't mind that you did things like
*infix_expr >= '0' && *infix_expr <= '9', but here are library functions for some of these things. If you are explicitly told you cannot use library functions, OK. Otherwise, this would be a good time to start learning about library functions.Speaking of library functions, you might want to look at
strtok.Your
switchstatements should havedefaultcases. This assures people reviewing your code that you have designed for the contingency of a missed pattern. Let me give you an example. I have fruit. Apples, bananas, pears. I perform different actions on each fruit I get. Eventually, a design change happens and my program is now supposed to process guavas. If I pass a guava to my switch statement, it won't handle the guava but also won't alert me that I gave the program an unhandled but valid fruit.Next, I generally discourage
typedef struct. You were probably told that it makes the code clearer, but I find that it encourages bad coding style. Yes, writingstruct Tokenmay seem ugly and tedious, but you never have any confusion with the type whereas you may if you change the typedef to include pointers or a different type altogether. I wish the language did not include thetypedefkeyword, but keep this in mind as you write C (and other languages that use this sort of keyword).In general, try to avoid using global variables. Your code as-is locks in use of the
stackvariable. Things like this should be in function parameters instead, or if you must have them all in one place, I recommend a structure for them:As an example on how this would change your code, let's fix your
popfunction. The function is supposed to return the last value on the stack, right? But also, what happens if you try to pop the stack when the stack is empty? Here's one way to write this.Note that I could have combined those last two non-return statements into two, but I like my code to be readable. I find it also makes debugging easier. Also note that my function commentary is pretty thorough.
Rearrange your tokenize function's while loop and add a NULL check:
You'll see that I like to add newlines in my code that separate ideas. I suggest you do the same. This includes adding newlines after functions.
I think that's enough commentary for now. If you have any questions, let me know.
1
u/Brilliant-Orange9117 Feb 18 '26
Use getline() to read lines and allocate the buffer on the heap for you.
1
u/Key_River7180 Feb 19 '26
//puts()could be useful for loggingcontinues a switch1
u/tinker-fox May 30 '26
You've gotten plenty of comments on style, etc, some of which you'll heed, some you'll ignore. I just wanted to mention that I enjoyed reading your code, since I've been doing a lot of work lately on my own calculator, which I started a long time ago. It used to look a lot more like yours (though it was originally RPN-only), but it's grown into quite a bit more than that. Sometimes when I'm working on it I wish it were simpler again. 😄
https://github.com/foxharp/rca
(Oh -- and yes, the C language guarantees that uninitialized globals are zero to start. So even without the "= {0}", your stack[] array would have been all-zero. If it had been declared inside a function, i.e., on the C runtime stack, then it would _not_ be initialized.)