HiveBrain v1.2.0
Get Started
← Back to all entries
patterncModerate

Simple calculator in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplecalculatorstackoverflow

Problem

I made a very simple calculator in C that allows the user to perform calculations like addition, subtraction, multiplication and division. This is my first C program that I've made outside of my University course. It would be great to get some feedback on this program so that I can avoid bad practices early on in my coding.

```
//Calculator program
#include
#include

int main()
{
int menu; //Variable for the number the user inputs
float num1, num2, result; //Float variables for the user input and output, used floats in case the user enters e.g. 14.7

printf("Enter a number from the list below\n\n");

printf("1. Addition\n"); //Calculator menu, user must enter a value from 1 - 4 for the program to work
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n\n");

printf("Enter number: "); //User input for the calculator menu
scanf("%d", &menu);

printf("\n");

switch(menu) //switch statement for menu
{
case 1:
printf("You entered Addition\n\n");

printf("Enter first number: "); //User input for first number
scanf("%f", &num1);

printf("Enter second number: "); //User input for second number
scanf("%f", &num2);

printf("\n");

result = num1 + num2; //Addition calculation

printf("%.2f + %.2f = %.2f\n", num1, num2, result); //Addition output
break;
case 2:
printf("You entered Subtraction\n\n");

printf("Enter first number: "); //User input for first number
scanf("%f", &num1);

printf("Enter second number: "); //User input for second number
scanf("%f", &num2);

printf("\n");

result = num1 - num2; //Subtraction calculation

printf("%.2f - %.2f = %.2f\n", num1, num2, result); //Subtraction output
break;
case 3:
printf("You entered Multiplication\n\n");

printf("Enter first number: "); //User input for first number
scanf

Solution

I must say, I respectively disagree with @ishyfishy's example of the addition function. Although, I do agree with the point about splitting the arithmetic functions away from the main function.

I believe that the arithmetic functions should only handle performing the arithmetic; all the I/O should be handled by main function (I have been taught that this is a good practice, although I do not have a source right now).

So, the "1" (addition) section of the switch statement in main would look something like this:

scanf("%f", &num1);
scanf("%f", &num2);

result = addition(num1, num2);

printf("...", num1, num2, result);


And the addition function simply looks like this:

float addition(float num1, float num2) {
    return num1 + num2;
}


This doesn't make a much of a difference, but it speeds up your code a little bit.

Instead of storing the result of a calculation in result, it would be faster to just pass the return value an arithmetic functions into the statement/function that you will it next in.

For example, the end of the "1" (addition) section of the switch statement would look like this:

printf("%.2f + %.2f = %.2f\n", num1, num2, addition(num1, num2));


This is faster because it reduces memory interaction(however, not by much).

In this section:

printf("Enter a number from the list below\n\n");

printf("1. Addition\n"); //Calculator menu, user must enter a value from 1 - 4 for the program to work
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n\n");


You make 5 calls to the printf function when you could easily just put all the strings into one and split them up by newline in a single printf call:

printf("Enter a number from the list below\n\n"
       "1. Addition\n"
       "2. Subtraction\n"
       "3. Multiplication\n"
       "4. Division\n");


This is also faster as you don't have to call a function 5 times.

Only use printf when you are need to use the formatting feature. Other than that, you are over-complicating things.

If you merely need to output a string with a newline here or there, use the built-in puts function; it automatically outputs a string with a newline on the end (and you can add more if needed).

For example, this line:

printf("You entered Addition\n\n");


Becomes:

puts("You entered Addition\n");

Code Snippets

scanf("%f", &num1);
scanf("%f", &num2);

result = addition(num1, num2);

printf("...", num1, num2, result);
float addition(float num1, float num2) {
    return num1 + num2;
}
printf("%.2f + %.2f = %.2f\n", num1, num2, addition(num1, num2));
printf("Enter a number from the list below\n\n");

printf("1. Addition\n"); //Calculator menu, user must enter a value from 1 - 4 for the program to work
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n\n");
printf("Enter a number from the list below\n\n"
       "1. Addition\n"
       "2. Subtraction\n"
       "3. Multiplication\n"
       "4. Division\n");

Context

StackExchange Code Review Q#91740, answer score: 14

Revisions (0)

No revisions yet.