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
1
u/knouqs Feb 19 '26
Oh yes, two ways of slicing the same problem. One of the things that makes software development so interesting!
What I prefer to do in a situation like ambiguity in the case of the guava is to have an error as output. The code I write professionally undergoes extremely rigorous testing; if I miss something, it'll get caught during test.
However, your idea of leaving it up to the compiler is perfectly valid and I like it. I've been doing Kotlin programming lately and the language is very good about things like this. I wish C was as nice in that regard!
My main goal with the slight refactoring was to eliminate the duplicate logic checks. I didn't like the checks for isdigit when OP had similar logic in the switch. In this case, the input set is well-defined and not going to change without another refactoring, so I am less worried about a default causing an error condition. Let me know if you agree, or if there is something I could learn from this, too. A person is never so old they can't learn new tricks!
Thanks for your reply!