this post was submitted on 22 Nov 2025
93 points (100.0% liked)

Programming

23594 readers
154 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
 

Please excuse - and do not hesitate to point out - any violation against etiquette that I might be committing here... I am new here.

I started to learn C a few months ago as a hobby as part of a bigger project, namely to learn about computers in general. I have had so much fun reading Code - The Hidden Language of Computer Hardware and Software by Charles Petzold. But that's another story...

I was about to buy a few new SSDs and needed to do some budgeting. Instead of using my phone's calculator, I decided to try to write a calculating program in C, because I hadn't touched programming for some weeks or months because life and I wanted to see if my knowledge had matured some.

The goal was to have it do the four standard arithmetics and also save the last result in a variable, which I just called "memory" for lack of bette phrasing on my part. Maybe next week I'll figure out how to make it possible to use the value saved in memory instead of having to type a number.

I welcome any constructive criticism on how and why this code is up to code or not(sorry...), if it can be improved and how or even if it's just garbage and why that is. I am just proud that it worked without gcc throwing any errors.

#include <stdio.h>

int main(void) {

        int num1 = 0;
        int num2 = 0;
        int choice = 0;
        int memory = 0;

        printf("Welcome to the Calculator of the century!\n\n");

        while (1) {
                printf("What would you like to do?\n\n");
                printf("(1) Add two numbers\n(2) Subtract two numbers\n(3) Multiply two numbers\n(4) Divide two numbers\n(5) Show memory\n(6) Exit\n\n");
                printf("Enter 1, 2, 3, 4, 5 or 6: ");
                scanf("%d", &choice);

                if (choice >= 6 || choice < 1) break;

                if (choice == 5) {
                        printf("\n%d in memory.\n\n", memory);
                } else if (choice < 5 || choice > 0) {
                        printf("\nEnter the first number: ");
                        scanf("%d", &num1);
                        printf("Enter the second number: ");
                        scanf("%d", &num2);
                }

                if (choice == 1) {
                        printf("\nThe sum of %d and %d is %d\n\n", num1, num2, num1 + num2);
                        memory = num1 + num2;
                } else if (choice == 2) {
                        printf("\nThe difference of %d and %d is %d\n\n", num1, num2, num1 - num2);
                        memory = num1 - num2;
                } else if (choice == 3) {
                        printf("\nThe product of %d and %d is %d\n\n", num1, num2, num1 * num2);
                        memory = num1 * num2;
                } else if (choice == 4) {
                        printf("\nThe quotient of %d and %d is %d\n\n", num1, num2, num1 / num2);
                        memory = num1 / num2;
                }
        }

        printf("\nWe hope to see you soon again!\n");
        return 0;
}
you are viewing a single comment's thread
view the rest of the comments
[–] henfredemars@infosec.pub 58 points 4 days ago* (last edited 4 days ago) (5 children)

Sr. Software Engineer here!

  • Great job initializing your variables. That's a surprisingly common source of bugs, and with today's optimizing compilers it's basically free to define the starting value at the beginning of your function. Don't worry if you reassign the value later. The compiler will notice and won't waste any time doing extra setup unless it actually matters.
  • Consider providing an error message for invalid inputs. Humans can be boneheaded and not realize they gave an invalid input.
  • Your code isn't commented. I recommend considering adding a few to build good habits early if you think you could comment something helpful for the reader. Code is read far more often than it is written ideally.
  • You perform your math operations twice: once in the printf and again later when you assign memory. Consider doing the computation just once to avoid repeating yourself to the computer. This habit tends to produce more efficient programs. Just update memory first and then reference it directly in your call to printf. This also protects against bugs where the value displayed wasn't really the value written to memory.
  • Consider checking for division by zero and provide an error message for the unreasonable request.
  • Maybe return a non-zero status if an error occurs.

Nice work!

[–] emotional_soup_88@programming.dev 16 points 4 days ago (1 children)

Thank you! I really appreciate your guidance! It almost feels like you could have experience from teaching or the likes, since you are so good at explaining! Or maybe that just comes with being a senior SE? Either way, thanks again!

[–] henfredemars@infosec.pub 10 points 4 days ago (1 children)

You're kind! I enjoy reviewing and talking about code. Anytime.

[–] emotional_soup_88@programming.dev 6 points 4 days ago (1 children)

Signing off now, but I did have the strength left to do this:

else if (choice == 4) {
                        if (num2 == 0) printf("\nhttps://en.wikipedia.org/wiki/Division_by_zero\n\n");
                        else {
                        memory = num1 / num2;
                        printf("\nThe quotient of %d and %d is %d\n\n", num1, num2, memory);
                        }
                }

Cheers!

[–] Oisteink@lemmy.world 9 points 4 days ago (1 children)

Your code wants to know if you’ve read up on switch statements.

Great work!!

Looking into it now! Thanks! :)

load more comments (3 replies)