C examples for Operator:Arithmetic Operator
The following table lists C's arithmetic operators.
Operator | Action |
---|---|
- | Subtraction, also unary minus |
+ | Addition |
* | Multiplication |
/ | Division |
% | Modulus |
-- | Decrement |
++ | Increment |
When you apply / to an integer or character, any remainder will be truncated. For example, 5/2 will equal 2 in integer division.
The modulus operator % yields the remainder of an integer division. However, you cannot use it on floating-point types.
The following code fragment illustrates %:
#include <stdio.h> int main(void) { int x, y;//from ww w.jav a2s.c o m x = 5; y = 2; printf("%d \n", x / y); /* will display 2 */ printf("%d \n", x%y); /* will display 1, the remainder of the integer division */ x = 1; y = 2; printf("%d %d \n", x / y, x%y); /* will display 0 1 */ return 0; }
The unary minus multiplies its operand by -1. That is, any number preceded by a minus sign switches its sign.