From the course: C Essential Training
Working with math operators - C Tutorial
From the course: C Essential Training
Working with math operators
- [Instructor] In this code, you see the four basic math operators in C: addition, subtraction, multiplication, and division. These symbols are standard in all of computerdom as well as programming. Build and run. And the computer does the math for you. Your job is only to get the equation correct. In this code, you see the increment and decrement operators at lines 11 and 12 respectively. The increment operator adds one to a value. The decrement operator subtracts one. These operators are the equivalent of these expressions. So A equals A plus one or B equals B minus one. These expressions work because the math is done on the right side of the equal sign first. Whatever value is resulting then gets assigned to the variable on the left. The increment and decrement operators, however, offer a convenient shortcut. The increment and decrement operators can be prefixed or post-fixed. Now, most often you're going to see them post-fixed as shown here at the end of line 10. But the position does affect the values. Build and run the code. You see is that the increment and decrement operations take place after the variable is used. If you want to increment or decrement before a variable is used, you must prefix the operators. So I shall edit. The increment operator goes on the left side as well as the decrement operator. Save this change. Build and run. And you see that the values are modified before they're used. The modulus operator obtains the modulo of two values or the remainder when the first value is divided by the second. So in this code, you see the modulus operator used at line 11. The modulus operator is the percent sign. This is modulus. It doesn't calculate a percentage. The remainder of whatever X divided by variable mod is is assigned to variable A. The printf statement then outputs the three values showing the equation and the result. Build and run. Now I'll try with two first. And there you see the modulo for two. The effect is that every other item is one or zero. Let me run it again and I'll try three as a value. And here you see the remainders of various values divided by three. Multiples of three always end up being zero. That's one of the keen aspects of using the modulo operator. And I would encourage you to run this code a few times with different values to see the effect the modulus operator has. It's not as common as the other basic math operators but it comes in really handy for computer programming.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Working with math operators3m 3s
-
(Locked)
Challenge: Do some math49s
-
(Locked)
Solution: Do some math1m 29s
-
(Locked)
Using assignment operators1m 44s
-
(Locked)
Obeying the order of precedence3m 13s
-
(Locked)
Challenge: Get the order correct54s
-
(Locked)
Solution: Get the order correct1m 11s
-
(Locked)
Working with relational operators3m 39s
-
(Locked)
Using logical operators3m 49s
-
(Locked)
Understanding bitwise operators3m 36s
-
(Locked)
Shifting bits3m 18s
-
(Locked)
Exploring unary operators3m 44s
-
(Locked)
Challenge: Binary math1m 5s
-
(Locked)
Solution: Binary math2m 11s
-
-
-
-