Operators in C
Following are the different types of operators used in c programming
Arithmetic operators
Relational operators
Logical operators
Increment and Decrement operators
Assignment operators
Bitwise Operator
Conditional operators
Arithmetic operators: ( + - * / % )
These operators are used to perform various arithmetic operations such as
addition, subtraction, multiplication, division, and modulus.
Ex. float a=8.0,b=4.0,
c;
c=a/b;
/* gives output 2.0 because quotient is 2.0*/
c=a%b; /* gives output 0 because remainder is zero
*/
Relational Operators: ( < > <= >= == != )
These operators are used to check the relation between the values the above table shows various relational operators.
Sometimes confusion may occur in equal to operator (==) and assignment operator(=) both having different meanings in c programming.
Equal to operator just check equality while the assignment operator assigns the value which
is at the right
of = sign to the variable at the left
side of = sign.
Logical Operators: ( && ||^)
The logical AND operator (&&) evaluates the truth or falseness of pairs of expressions. if both expressions are true, the logical AND operator
returns 1.
Otherwise, the operator returns 0.
However, the logical OR operator (||) returns 1 if at least one of the expressions is true. The || operator returns 0 if both expressions
are false.
Only one operand (or expression) can be taken by the logical negation operator (!).
If the operand is true, the (!) operator returns 0; otherwise, the operator returns 1
Increment and Decrement operators
: ( ++ -- )
The increment and decrement operators are ++ and -- respectively we use these increments or decrement the value of an associated variable by 1 only.
The increment/decrement operators may be of the prefix (before variable) or postfix form (after variable)
Prefix form: Ex. Assuming i as variable
++i; is equivalent to i=i+1;
--i; is equivalent to i=i-1;
Postfix form:
i++; is equivalent to i=i+1;
i--; is equivalent to i=i-1;
The difference between prefix and postfix forms observed when these operators are used as part of a larger expression
or in the value assignment case.
If (++i) is used in an expression,
it means the value of ‘i’ is incremented before the expression is evaluated.
If (i++) is used in an expression, that means the value of ‘i’ is incremented after the expression is
evaluated.
Ex. Assume that the integer variables i is initialized to a value 2
Then in the following statement
x=++i; /* Gives output i=3 and x=3*/
x=i++; /* Gives output i=3 but x=2
because here we are applying the increment operators after the variable name ‘i’ */
Assignment Operator: ( =
)
The assignment operator is a simple (=) sign.
It is used to assign values to a variable,
The expression may be a variable, a value, literals, and operators combination.
The assignment operation always takes from the right to the left.
Ex. a=b+c; /* value of ‘b’ added with ‘c’ the result assigned to ‘a’ */
The shortcut can be possible with assignment operators like,
a=a+b; /* this can be written as a+=b;
Bitwise Operator:( & | ^ ~ )
Using C you can access and manipulate the bits in the variables you can perform bitwise
AND, OR, XOR, Complement
operation as well right shifting and left shifting of bits. Ex.
the following table shows the truth table for AND, OR, XOR.
Shift Operators: (<< >>)
Right Shift (>>) - It will shift the bits
of operand towards the right; the blanks are created on the left side which is filled by zero(s). In general, declared as
Variable >> number of bits
Ex. if the variable a contains the bit pattern 11010011, then, a >> 1 would give
01101001
and a>> 2 would give 00110100.
Left Shift (<<) - It will shift the bits of operand towards the left; the blanks are created on the right side which is filled by zero(s). In general, declared as:
Variable << number of bits
if the variable a contains the bit pattern 11010011, then, a << 1 would give
10100110and a<< 2 would give 01001100
Conditional Operators: ( ? :)
In the conditional operator, we are writing three expressions, the first expression1
is always a condition that is to be checked, if this condition is true then expression2 will be selected as output, if the condition is false then expression3 will be selected as
the output.
Ex. int a=15,b=5;
(a>b)?
printf(“a is larger”) : printf(“b is larger”);
In this case output will be
a is larger