C operators

An operator is a symbol that indicates or tells the compiler for specific operations on given operands. Like assignment operator ‘=’ is used to assign a value.There are several operators in C language to perform various operations. These operators are categories as given below.Types of operator
  • Arithmetic operator
+,  -,  *,  /,  %
  • Relational operator
<, <=,  >,  >=
  • Equality operator
==,  !=
  • Logical operator
&&,  ||,  !
  • Assignment operator
=
  • Unary operator
-,  ++,  –,  sizeof
  • Bit wise operator
&,  |,  ^,  ~,  <<,  >>
  • Ternary operator or Conditional operator
? :
Assignment operator assign a value to a variable
Example:-
int money;  /* Here we don’t know exact value of variable ‘money’ */
money = 10;  /* Using assignment operator we assign 10, now value of ‘money’ is 10*/

We wiil see below some example of arithmetic operators

4 + 5     = 9;
5 – 4      = 1;
5 * 4      = 20;
20 / 4     = 5;
21 / 4     = 5;
25 / 6     = 4;
20 % 4   = 0;
21 % 4   = 1;
25 % 10 = 5;
Remember modulus( % ) operator always gives remainder.

Use of  unary operators.

Use of increment ( ++ ) operator
a++; (This is called post increment, the value of a is incremented by one after executing this statment)
++a; (This is called pre increment, the value of a is incremented by one before executing this statment)Use of decrement ( — ) operator
a–; (This is called post decrement, the value of a is decremented by one after executing this statment)
–a; (This is called pre decrement, the value of a is decremented by one before executing this statment)sizeof operator is used to calculate the size of datatypes in number of bytes, sizeof can be applied to all datatypes.Note :- if you did not understand, no need to worry as we will use these operators in C program things will be more cleared.

Here I would like to explain bit wise operator in little detail because these are most useful operators in C.
&    –    Bitwise AND
|    –    Bitwise OR
^    –    Bitwise EX-OR
~    –    one’s complement

Bitwsie AND

a        b        a&b

0       0             0
0        1            0
1        0            0
1        1            1

Bitwise OR

a        b        a|b
0        0           0
0        1           1
1        0            1
1        1            1

Bitwise EX-OR

a        b        a^b
0        0            0
0        1           1
1        0           1
1         1           0

Bitwise operators performs on one or more bit patterns or binary numerals levels of their indivisual bits.

Example :-
3 & 5   =  1         /*Here we do AND operation on bit representation of 3 and 4. */
3 ->  0011
&
4->   0101
1->   0001

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.