C – Basic Arithmetic Operators

Course Outline  | Previous LessonNext Lesson

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

Addition is denoted as the plus symbol (+). The code below demonstrates the use of adding numbers together. The sum is stored into a variable.

#include <stdio.h>

int main() {
    int sum = 1 + 2;
    printf("Result: %d\n", sum);
    return 0;
}

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.

#include <stdio.h>

int main() {
    int num1 = 5;
    int num2 = 4;
    int sum;

    sum = num1 + num2;
    printf("Result: %d\n", sum);
    return 0;
}

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.

#include <stdio.h>

int main() {
    float num1 = 1.02;
    float num2 = 2.08;
    float sum;

    sum = num1 + num2;
    printf("Result: %0.2f\n", sum);
    return 0;
}

Output: Result: 3.10. The program above works the same as adding integers together but with real numbers.

Subtraction

The subtraction symbol is donated as the dash symbol (-). The examples below demonstrate the use of the subtraction symbol.

#include <stdio.h>

int main() {
    int difference;
    
    difference = 20 - 10;
    printf("Result: %d\n", difference);
    return 0;
}

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.

#include <stdio.h>

int main() {
    int num1 = 30;
    int num2 = 20;

    int difference = num1 - num2;
    printf("Result: %d\n", difference);
    return 0;
}

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.

#include <stdio.h>

int main() {
    float num1 = 4.10;
    float num2 = 2.10;

    float difference = num1 - num2;
    printf("Result: %0.2f", difference);
    return 0;
}

Recall that 0.2 from the placeholder limits the number of digits after the decimal point to two. Output is: Result: 2.00.

Multiplication

Multiplication is possible to operate in C, and the multiplication symbol is denoted as the asterisk (*).

#include <stdio.h>

int main() {
    int result;

    result = 10 * 2;
    printf("Result: %d\n", result);
    return 0;
}

Output: Result: 20. This program works the same way as the previous examples but with multiplication.

#include <stdio.h>

int main() {
    int num1 = 9;
    int num2 = 3;

    int result = num1 * num2;
    printf("Result: %d\n", result);
    return 0;
}

Output: Result: 27. This program also works the same way as the previous examples.

#include  <stdio.h>

int main() {
    float num1 = 7.5;
    float num2 = 2;

    float result = num1 * num2;
    printf("Result: %d\n", result);
    return 0;
}

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.

Exponentials

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.

// With integers
int y = 2*2*2;

// With variables
int x;
int z = x*x*x;

Variables y and z are equivalent to 2^3 and x^3 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 x^2 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

Division is also possible to operate in C. The division symbol is denoted as the forward slash (/).

#include <stdio.h>

int main() {
    int result;

    result = 4 / 2;
    printf("Result: %d\n", result);
    return 0;
}

Output: Result: 2.

#include <stdio.h>

int main() {
    int num1 = 10;
    int num2 = 2;

    int result = num1 / num2;
    printf("Result: %d\n", result);
    return 0;
}

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.

#include <stdio.h>

int main() {
    float num1 = 1;
    float num2 = 2;

    float result = num1 / num2;
    printf("Result: %d\n", result);
    return 0;
}

The output is: Result: 0.5. If int data type is used, the answer would be zero.

Modulus

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.

#include <stdio.h>

int main() {
    int result;

    result = 5 % 2;
    printf("Result: %d\n", result);
    return 0;
}

The output is: Result: 1. The number 1 is the remainder of 5 divided by 2.

#include <stdio.h>

int main() {
    int num1 = 6;
    int num2 = 13;

    int result = num1 % num2;
    printf("Result: %d\n", result);
    return 0;
}

Output: Result: 6.

Increment and Decrement

It is possible to add a number to an existing value of a variable.

#include <stdio.h>

int main() {
    int num = 1;
    num = num + 1;
    return 0;
}

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.

Assignment Operators

There is a short-hand notation for the process above.

#include <stdio.h>

int main() {
    int num;
    num += 1;
    return 0;
}

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.

Short-Hand Notation

There is a way to shorten the code even further. Please refer to the code below.

#include <stdio.h>

int main() {
    int num = 1;
    num++;
    return 0;
}

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.

#include <stdio.h>

int main() {
    int x = 1;
    printf("%d\n", x++);
    return 0;
}

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.

int y = 2*((x - 2)/3) + 20;

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.

Conclusion

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.

Course Outline  | Previous LessonNext Lesson

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz