Arithmetic Operators
Arithmetic operators are used to conduct mathematical operations in your code. The chart below contains the different types of arithmetic operators you can use in C:
Addition
The first one, “ + ”, is the addition operator, which allows you to add numbers together. You can use it with regular numbers, and with variables too.
Examples:
int num = 21 + 46;
int num1 = 21; int num2 = 46; int num3 = num1 + num2;
Subtraction
The second operator, “ - ”, is the subtraction operator, which allows you to subtract a number from another. Like the addition operator, you can use it with regular numbers, and with variables too.
Multiplication
The third operator, “ * ”, is the multiplication operator, which allows you to multiply numbers together. Like the operators we have discussed already, you can use it with regular numbers and with variables too.
Division
The fourth operator, “ / ”, is the division operator, which allows you to divide a number by another. Like the first three operators, you can use it with regular numbers and with variables too.
Modulus
The last arithmetic operator on this chart, “%”, is the modulus operator. Chances are you haven’t seen this one before, so let’s clarify what this one does. When you divide a number by another number, you are not always going to have them divide into each other perfectly. Sometimes, you end up with a remainder. The modulus operator allows you to store the remainder of two numbers or variables in another variable. This one might seem a little tricky at first, so here are some examples:
Practice
Arithmetic
Create a num variable and create a mini calculator by using every arithmetic operator on it.
Solution Code
#include <stdio.h> int main(void) { int num = 3; num = 3+1; num = 3-1; num = 3*1; num = 3/1; }
Next Section
Copyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.