C – Basic Arithmetic Operators
Introduction to Arithmetic Operators
Computer programs can perform basic, arithmetic operations like adding, subtracting, and more. In addition, variables are mostly involved with with these operations. This concept is important because the code used in commercial products use arithmetic operators for many tasks.
This lesson covers basic, arithmetic operators with variables and the short-hand notation of the mathematical operations.
Addition is denoted as the plus symbol (+). The code below demonstrates the use of adding numbers together. The sum is stored into a variable.
The output is: Result: 3. The program is adding 1 with 2. Then the result is stored into the sum variable. The printf function is used to display the sum to the screen.
The code below demonstrates the use of more variables.
The output is: Result: 9. The num1 and num2 values do not change. Rather, the program is using the values, and it adds them together. As a result, the sum is stored into the sum variable.
Real numbers can also be added that are not integers.
Output: Result: 3.10. The program above works the same as adding integers together but with real numbers.
The subtraction symbol is donated as the dash symbol (-). The examples below demonstrate the use of the subtraction symbol.
Output: Result: 10. This program works the same as the addition operator. Integer 10 is subtracted from 20, and the difference is stored into the difference variable.
Output: Result: 10. Remember that values from num1 and num2 variables do not change. The program uses the values and performs a mathematical operation. The result is stored into the difference variable.
Real numbers can be used in this case as well.
Recall that 0.2 from the placeholder limits the number of digits after the decimal point to two. Output is: Result: 2.00.
Multiplication is possible to operate in C, and the multiplication symbol is denoted as the asterisk (*).
Output: Result: 20. This program works the same way as the previous examples but with multiplication.
Output: Result: 27. This program also works the same way as the previous examples.
The output is: Result: 15. If the result variable is an int instead, the output would still be 15. However, if num1 is of int data type and everything else is the same, the result would be 14. Variable num1 would round down 7.5 to 7.
Division is also possible to operate in C. The division symbol is denoted as the forward slash (/).
Output: Result: 2.
Output: Result: 5.
It is best to use float or double data type to compute division. For an example 1 divided 3 is 0.333333. If data type int is used, the result would be zero.
The output is: Result: 0.5. If int data type is used, the answer would be zero.
The modulus operator performs the same as the division operator. Instead of getting the result from the division, the result comes from the remainder of the division. For an example, 1/2 would give the remainder of 1. Finally, the modulus operator is denoted as the percent symbol (%). This operator only works with variables of data type int. Please refer to the code below.
The output is: Result: 1. The number 1 is the remainder of 5 divided by 2.
Output: Result: 6.
Increment and Decrement
It is possible to add a number to an existing value of a variable.
Variable num on the right-hand side of the = symbol is still 1. Then that variable is being added with an integer 1, which produces integer 2. Note that num still has a value a 1 until the variable num on the left-hand side is reassigned to integer 2. After that happens and when that process happens again, then num on the right-hand side would be 2, and that number is being added to 3. Thus, that new value to being assigned to num.
This can also be done with all arithmetic operators such as subtraction and others.
There is a short-hand notation for the process above.
The code above is the same thing as the previous code. This process is called the assignment operator. Other arithmetic operators can be used as shown earlier. Replace the addition operator with other operator signs for practice.
There is a way to shorten the code even further. Please refer to the code below.
This process is also the same as the previous code, but only integer 1 can be added to the variable in this notation. Thus, this short-hand notation is the post-increment operator (x++ where x is any variable). The example below uses the post-increment operator.
The output of the program above would be 1. The output is not 2 because the variable is only going to be added after the program uses x first. If a user wants to add a number before using x, he or she would have to use pre-increment operator (++x). Then the output would be 2.
It is also possible to subtract one from the variable. That process is called decrement. Like post and pre-increment operators, there are post-decrement and pre-decrement operators (x– and –x respectively) that behaves the same as the incremental operators but with subtraction.
Arithmetic operators are used to perform mathematical operations within computer programs. Furthermore, computers can add, subtract, multiply, divide, and take the remainder. Programs can also use variables for mathematical operations as shown earlier. Short-hand notations are used to make typing easy, which includes incremental and decremental operators.