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 this lesson also covers the short-hand notation of the mathematical operations.
Recall that the equal sign in the programs below does not represent equality in C. The right-hand side of the equal sign is an expression that is assigned to a memory space by using a variable. Also recall that a variable refers to a physical location in memory.
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. Then 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.
There are two ways to compute exponential functions, but only one method will be mentioned in this lesson. The second method will be covered in a future lesson.
The first method is have a number or variable that multiplies with itself one or more times. For an example, please see the code snippet below.
Variables y and z are equivalent to and respectively. More complex exponential functions will be covered in a future lesson with the second method.
Please note that this symbol (^) does NOT raise to a power of any number. Thus, this expression is not equivalent to this, x^2, in C. That symbol actually has a different functionality, and that will also be covered in a future lesson.
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.
Order of Operations
More than one, arithmetic operators can be used to form a single expression, which means an order of operations is involved. Like in mathematics, PEMDAS would be used to calculate any complex expression properly in C. Please visit the PEMDAS link to review the order of operations if needed. The code snippet below shows an example of multiple, arithmetic operators that are being used.
It is always a good idea to add parenthesis to expressions so each arithmetic operators are being computed in the correct order. Otherwise, expressions could produce unexpected results.
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.