r/cprogramming • • 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

link: https://github.com/hotfixx/newcalc

4 Upvotes

18 comments sorted by

View all comments

Show parent comment

1

u/simmepi Feb 19 '26

Yep, outputting error is also a good way to handle it, and I’ve sometimes used it as well. The most depressing situation was one where I was working with cross platform C code and among the compilers were Microsoft’s and Sun’s, and I had a switch case for an enum. So using the pattern I described, ie no default, everything was fine on Solaris. However, MS reported the missing default as a warning and you couldn’t configure the flags to make it ignore it. So I added a default to get rid of the warning → the Solaris compiler now complained about an unnecessary default (it was smart and realized I had explicitly handled all the values in the enum and hence the default was unreachable code). This was also not possible to configure so as to silence the warning…

1

u/knouqs Feb 19 '26

Ah, yes, in cases like that I've used the preprocessor directives for OS-specific code. Stuff like "#ifndef SUN_COMPILER" or whatever (I can't remember the flags at the moment, but you get the idea). I had to do that with AIX-vs-Red Hat cross compiling. My company would only approve code that didn't give warnings without specific standards checker exceptions, so I had to get creative and very specific sometimes.