Printing calculator - Stephen Kochan exercise I'm a newbie at programming, just one month into C. Working with Stephen Kochan's programming in C. There is a question in chapter 6 to write a printing calculator where 's' tells the program to set the accumulator, 'e' tells that the execution must end. And the operators tell the operations to be performed on the accumulations.

The sample run according to the book looks like this:


10 s
=10.000000
2 /
=5.000000
55 -
=-50.000000
100.25 S
=100.250000
0 E
=100.250000



I got this to work but I'm not sure about how correct this code is.

#include&lt;stdio.h&gt;
// write a program for a printing calculator
// exercise 4, chapter 6, kochan

int main(void)
{
 float value, output;
 char operator;
 _Bool exit;

 printf("Please enter a value and operator to begin\n");
 scanf("%f %c", &amp;value, &amp;operator);


while(operator != 'e' || operator != 'E')
{
   exit = 0;

 switch(operator)
 {
    case 'S':
    case 's':
    output = value;
    printf("= %.3f\n", output);
    scanf("%f  %c", &amp;value, &amp;operator);
    break;

    case '+':
    output = output + value;
    printf("= %3f\n", output);
    scanf("%f  %c", &amp;value, &amp;operator);
    break;

    case'-':
    output = output - value;
    printf("= %.3f\n", output);
    scanf("%f  %c", &amp;value, &amp;operator);
    break;

    case '*':
    output = output * value;
    printf("= %.3f\n", output);
    scanf("%f  %c", &amp;value, &amp;operator);
    break;

    case '/':
    if(value == 0)
    printf("Division by zero\n");
    else
    output = output / value;
    printf("= %.3f\n", output);
    scanf("%f  %c", &amp;value, &amp;operator);
    break;

    case 'E':
    case 'e':
    output = output + value;
    printf("= %.3f\n", output);
    exit = 1;
    return 0;

    default:
    printf("Unidentified operator\n");
    return 0;


  }
}

   return 0;
}
This seems correct, and it works. Some simplifications are possible.

Don't repeat yourself

These lines appear many times in the code:


printf("= %.3f\n", output);
scanf("%f  %c", &amp;value, &amp;operator);



You can move this part after the switch block to reduce duplication.

Remove unnecessary elements

The exit variable is written to but never read.
It seems unused, so I suggest to remove it.

Coding style

It would be easier to read if you indent the body of case statements.

It's recommended to use braces with all if-else statements.

I suggest to put a space after #include and before &lt;stdio.h&gt;

Suggested implementation

Putting together the suggestions above, your implementation becomes simpler and easier to read:

#include &lt;stdio.h&gt;

int main(void)
{
    float value, output;
    char operator;

    printf("Please enter a value and operator to begin\n");
    scanf("%f %c", &amp;value, &amp;operator);

    while (operator != 'e' || operator != 'E')
    {
        switch(operator)
        {
            case 'S':
            case 's':
                output = value;
                break;

            case '+':
                output += value;
                break;

            case'-':
                output -= value;
                break;

            case '*':
                output *= value;
                break;

            case '/':
                if (value == 0)
                {
                    printf("Division by zero\n");
                }
                else
                {
                    output /= value;
                }
                break;

            case 'E':
            case 'e':
                output += value;
                printf("= %.3f\n", output);
                return 0;

            default:
                printf("Unidentified operator\n");
                return 0;
        }
        printf("= %.3f\n", output);
        scanf("%f %c", &amp;value, &amp;operator);
    }

    return 0;
}