Operators in C
C has a rich set of operators (i.e., things like + – * / ), which allow you to write complicated expressions quite compactly. General expressions are formed by joining together constants and variables (operands) via various operators.
Operators in C fall into a number of classes:
Arithmetic operators, Unary operators, Relational and Logical operators, Assignment operators, Equality operators, and the Conditional operator.\
Unary Operators
Unary operators are operators that only take one argument.
+123 positive 123 -123 negative 123 !i logical negation (i.e., 1 if i is zero, 0 otherwise) ++i adds one to i, and returns the new value of i ––i subtracts one from i, and returns the new value of i i++ adds one to i, and returns the old value of i i–– subtracts one from i, and returns the old value of i
++i or i++ are also called increment or decrement operators. It only ads 1 to the value.
Example 1: Using
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int val=100; printf("Val is %d",val); // val is 100 val++; //val is 101 printf("\nVal is %d",val); ++val; //val is 102 printf("\nVal is %d",val); val--; //val is 101 printf("\nVal is %d",val); printf("\nVal is %d",++val);//here val is 102 printf("\nVal is %d",val++);//here val 102. prints 101 but increments is done after print //if you print here val will be 103 return 0; }
Binary Operators
Binary operators work on two operands (‘binary‘ here means 2 operands, not in the sense of base-2 arithmetic).
+ addition – subtraction * multiplication / division % remainder (e.g., 2%3 is 2), also called 'modulo' << left-shift (e.g., i<<j is i shifted to the left by j bits) >> right-shift & bit wise AND | bit wise OR ^ bit wise exclusive-OR && logical AND (returns 1 if both operands are non-zero; else 0) || logical OR (returns 1 if either operand is non- zero; else 0) < less than (e.g., i<j returns 1 if i is less than j) > greater than <= less than or equal >= greater than or equal == equals != does not equal ? conditional operator
Example 2:
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int a=10; int b=20; int c=a+b; printf("The sum is %d",c); return 0; }
Assignment Operators
The most common assignment operator in C is the equals operator, =. It is used to change the value of a variable. For instance, the expression: f = 3.4; causes the floating-point value 3.4 to be assigned to the variable f.
Multiple assignments are permissible in C. For example,
i = j = k = 4;
causes the integer value 4 to be assigned to i, j, and k, simultaneously.
Arithmetic Assignment Operators in C
= assignment += addition assignment -= subtraction assignment *= multiplication assignment /= division assignment %= remainder/modulus assignment &= bit wise AND assignment |= bit wise OR assignment ^= bit wise exclusive OR assignment <<= left shift assignment >>= right shift assignment
Example 3:
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int price=100; int quantity=2; price*=quantity; // it is equal to price=price*quantity printf("Total amount is %d",price); return 0; }
Arithmetic Operators
There are four main arithmetic operators in C:
addition + subtraction – multiplication * division /
There is no built-in exponentiation operator in C . Instead, there is a library function (pow) which carries out this operation.
e.g. x2 is represented as x * x
Operation | C Operator | Algebraic Expression | C Expression |
Addition | + | f + 7 | f + 7 |
Subtraction | – | a – b | a – b |
Multiplication | * | bm | b * m |
Division | / | x / y or x ÷ y | x / y |
Modulus | % | r mod s | r % s |
Relational Operators
Relational operators are used to compare two or more operators. The result of comparison is either true or false.
Symbol | Operator | Example |
< | Less Than | a < b |
> | Greater Than | a > b |
= = | Equal to | a = b |
<= | Less or Equal | a <= b |
>= | Greater or Equal | a >=b |
<> | Not Equal | a <> b |
Example 4:
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int cost=250; int price=300; printf("Answer is : %d",cost<price); printf("\n"); printf("Answer is : %d",cost==price); }
Output:
Answer is : 1 Answer is : 0
Logical Operators
Effects on relational expression’s results also AND, OR combines two or more Relational expressions, Results will be returned either TRUE( 1 ) or FALSE ( 0 ) regards with specific situations.
Symbol | Operator | Example |
AND | AND | A< b AND c > d |
OR | OR | a < b OR c > d |
NOT | NOT | NOT ( a < b ) |
Suppose we have the following values:
a = 2, b = 5, c = 9, d = 7
Symbol | Operator | Example |
AND | a< b AND c > d | 1 |
OR | a < b OR c > d | 1 |
NOT | NOT ( a < b ) | 0 |
Code Example
Support us by sharing this post