C is a famed programming language. It attained this position due to its unique features. You may have seen a Hello World program, But today, we are going to learn to write a C Program to Create a Simple Calculator.
You can use any IDE you prefer, but I prefer CLion. They even give CLion for free for an year if you are a student!
Full Code (main.c)
#include <stdio.h> int main() { while(1){ char operation_symbol; float num1 = 0; float num2 = 0; printf("\n\n\nPlease enter operation symbol or Enter 0 to exit program.\n"); scanf("%c", &operation_symbol); if (operation_symbol == '0'){ break; } else if (operation_symbol != '0'){ printf("\nPlease enter num1 : "); scanf("%f", &num1); printf("\nPlease enter num2 : "); scanf("%f", &num2); getchar(); switch (operation_symbol) { case '+': printf("%f", num1 + num2); break; case '-': printf("%f", num1 - num2); break; case '*': printf("%f", num1 * num2); break; case '/': printf("%f", num1 / num2); break; default: printf("ERROR"); } } } return 0; }
Code Explanation
Line 1
#include <stdio.h>
This imports the “stdio.h” file into the source code. stdio stands for “standard input & output”. This allows us to use the library functions like scanf() and printf().
Line 3
int main() {
This main() function is the firstly executing part of the program. It is called the Entry Point of the program. The int is the return type of the program. For the moment, think that almost all of the C programs have their main() function’s return type as int. If it returns 0, it means the program exited successfully. If it returned something else (say a negative value), it indicates some kind of problem in your program.
Line 4
while(1){
This is a while loop. A while loop executes the commands within it as long as the condition inside the parenthesis is true. In C, any non-zero value is logical 1. It simply means anything other than zero in C evaluates to true (logical 1). Here you could have used any non-zero value within the brackets (even negative values), but the standard convention is to use “1”.
The reason that we have used a while loop here, is to be able to carry out calculations multiply times until we choose to exit the program. If we do not use the while loop, the program works fine, but exits after just performing one calculation only.
Note that the while loop must be closed with a } at the end.
Lines 5 to 7
char operation_symbol; float num1 = 0; float num2 = 0;
These are the variable declarations. Notice how we have initialized num1 and num2 variables to value 0. But the initial value of operation_symbol variable is a garbage value.
Line 9
printf("\n\n\nPlease enter operation symbol or Enter 0 to exit program.\n");
This is the first output of the program that the user sees when the program is run. It asks the user to enter an operation symbol like +, -, *, / or 0. If user inputs a symbol, the relevant mathematical operation is carried out. If the user enters 0, then the while loop is stopped (due to break statement), and thereby stop the program.
Remember the printf() function is available to us because we imported the stdio.h file.
Line 10
scanf("%c", &operation_symbol);
To get input from user as a keyboard input to the console (or command prompt in windows), the scanf() function is used. This function takes 2 parameters, the format specifier and the storage address. There are many format specifiers in C, and “%c” stands for character. So, the input only expects a character. The & symbol at the beginning of a variable in C, says the program to return the address of that variable. This is a hexadecimal value.
The scanf() takes the input character given by the user, and stores in at the memory address of the operation_symbol variable.
Line 20
getchar();
Whenever we enter a character like ‘A’, ‘B’, ‘g’, ‘j’ (without quotes) and hit enter in the console, that enter keypress remains in the standard input stream (stdin) buffer. So any scanf() following the first scanf() gets that enter as its input. To sort of clear that enter press, we use getchar() function in C.
Line 22 to 37
switch (operation_symbol) { case '+': printf("%f", num1 + num2); break; case '-': printf("%f", num1 - num2); break; case '*': printf("%f", num1 * num2); break; case '/': printf("%f", num1 / num2); break; default: printf("ERROR"); }
The switch statement is like a selector. It executes the commands reserved for each case. The cases are the values that the expression inside the parenthesis can take. Here, the operation_symbol variable (a variable is a simple expression) can take values +, -, *, /. So for each case, there are several commands. In each case, there is a break statement. It says the program to not look for more cases if one case is satisfied. The default case is when none of the cases are satisfied. (value of operation_symbol variable is something other than +, -, *, /).
Conclusion
And that’s a C Program to Create a Simple Calculator Explained.
Great work rukshan. Really I’m very happy.
Thank you Nimasha
Great job brother! Keep it up!
Thank you Pabasara
Good job💪👌🔥
Thank you Wanuja