The same person as Daedskin@lemm.ee and Daedskin@programming.dev
- 0 Posts
- 1 Comment
Joined 5 months ago
Cake day: June 26th, 2025
You are not logged in. If you use a Fediverse account that is able to follow users, you can follow this user.
The same person as Daedskin@lemm.ee and Daedskin@programming.dev
One thing you can do that I find comes up pretty often in C is start using switch statements to avoid code duplication. Because your operators all take 2 numbers, you’ve separated out the input handling into its own block; that alone is a really good insight.
You can take it another step further by making your print statement also, mostly, one block of code, with the only difference being the operation being preformed. By printing “The” followed by using an if-else block — or, again, preferably a switch statement — to both print the operation text, and compute
memory, you can print the remainder of the line the same way for every operation.It may seem nitpicky for such a simple program; but the use case of C is for programs which require tight execution and often contain complex, low-level logic. Reducing complexity whenever possible helps make C more readable, as well as signal when the complexity is necessary.