C uses common characters as operators for basic math: + - * and / for addition, subtraction, multiplication, and division, as well as the % sign for the modulus operator, which obtains the remainder of one value divided into another. Crafting a mathematical expression in C is only half as difficult as you think because it's the computer that does the math.
- [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.
- Writing your first C program
- Working the C development cycle
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Making new data types
- Typecasting variables
- Using math, assignment, and logical operators
- Creating for and while loops
- Nesting loops
- Creating functions
- Returning values from functions
- Passing arguments to functions
- Creating recursive functions
Skill Level Intermediate
C: Data Structures, Pointers, and File Systemswith Dan Gookin2h 57m Intermediate
1. The Fundamentals of C
2. Variables and Values
3. Operators and Math
4. Decisions and Loops
Next steps1m 38s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.