6.4. Operators

In the previous examples, you could see some simple operators in use, adding and multiplying numbers and variables. C has a variety of different operators, some that you will need to use nearly every time you sit down to program, and others that you rarely see. The following table shows some of the most important arithmetic and logical operators in C.

OperatorSymbolDescriptionExample
Addition+Adds two numbers together.5.46 + 7.2
Subtraction-Subtracts second number from first.8 - 6
Multiplication*Multiplies two numbers together.7 * 19.5
Division/Divides the first number by the second.10 / 2
Modulus %Findsremainder after integer division.11 % 2
Logical OR||True if one or both expressions are true.1 || 0
Logical AND&&Only true if both expressions are true.1 && 1
Not!True if expression is false, and vice versa.!0
Increment++Increase integer variable by one.++i
Decrement−−Decrease integer variable by one.--i
Addition Assignment+=Add LHS to RHS and assign value to LHS.i += j
Subtraction Assignment−=Subtract LHS from RHS and assign LHS.i −= j
Assignment=Assign variable on LHS to RHS.i = 5
Equality==Test if two values are equal.1 == 1
Inequality!=Test if two values are not equal.1 != 1
Greater Than>Test if first value is greater than the second value.10 > 5
Less Than<Test if first value is less than the second value.10 < 5
Greater or Equal>=Test if first value is greater than or equal to the second value.10 >= 5
Less or Equal<=Test if first value is less than or equal to the second value. ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.