Best post to learn Operators in C part 2

Operators in C

Operators are symbols which are use to perform operations on data, operations like arithmetic, assignment, unary, etc.

Operators helps us to manipulate, comparison, Calculate data in program. This help us to make decision and control the flow of program.

Following are the types of operators in C program.

  1. Unary Operators
  2. Arithmetic Operators
  3. Relational Operators
  4. Logical Operator
  5. Ternary Operator
  6. Assignment Operator
  7. Bitwise Operator
All operators has their own precedence and associativity. 
ITVoyagers

Ternary/ Conditional Operators

ternary-conditional-operator-itvoyagers

One line if-else..

As the name suggests conditional operator is use to check condition.

Ternary Operator as the name says it works on 3 operand.

Syntax:

    Condition ? Statement_1 : Statement_2

 

Here condition is checked and if condition is valid(true) then conditional operator will run Statement_1 and if condition is invalid(false) then conditional will run Statement_2.

Ternary / Conditional operator “?:”

We can check condition statement or variable. If the value of variable in non-zero then it will execute Statement_1 or else it will execute Statement_2.

Syntax :

    Condition ? Statement_1 : Statement_2

Example 1 :

    int a = 8, b=3, c;

    c = a > b ? 5 : 7;                      // If condition is true then store 5 in c or else store 7

    printf(“Value stored in C is = %d”,c);

Output :

    Value stored in C is = 5

 

Example 2 :

    int a = 8, b=3, c;

    c = a ? 5 : 7;                      // If the value of variable in non-zero then store 5 in c or else store 7

    printf(“Value stored in C is = %d”,c);

Output :

    Value stored in C is = 5

ITVoyagers

Assignment Operators

assignment-operator-itvoyagers

Assignment operator is use to assign values to variables.

We can also update value of a variable.

Assign “=”

Assigns the value to a variable.

Syntax :

  operand = Value

Example :

  int a = 8;

  printf(“Value of a is = %d”,a);

Output :

  Value of a is = 1

 

Add and assign “+=”

It will add value to variable’s value and stores back in same variable.

Syntax :

  operand += Value

Example :

  int a = 8;

  a+=2;      // It means a = a + 2

  printf(“Value of a is = %d”,a);

Output :

  Value of a is = 10

 

Subtract and assign “-=”

It will subtract value from variable’s value and stores back in same variable.

Syntax :

  operand -= Value

Example :

  int a = 8;

  a-=2;      // It means a = a – 2

  printf(“Value of a is = %d”,a);

Output :

  Value of a is = 6

 

Multiply and assign “*=”

It will multiply value with variable’s value and stores back in same variable.

Syntax :

  operand *= Value

Example :

  int a = 8;

  a*=2;      // It means a = a * 2

  printf(“Value of a is = %d”,a);

Output :

  Value of a is = 16

 

Divide and assign “/=”

It will divide variable’s value from mention value and stores back quotient in same variable.

Syntax :

  operand /= Value

Example :

  int a = 8;

  a/=2;      // It means a = a / 2

  printf(“Value of a is = %d”,a);

Output :

  Value of a is = 4

 

Not equal to “!=”

It will divide variable’s value from mention value and stores back remainder in same variable. In simple words it will perform modulo operation on variables value.

Syntax :

  operand %= Value

Example :

  int a = 8;

  a%=3;      // It means a = a % 3

  printf(“Value of a is = %d”,a);

Output :

  Value of a is = 2

 

ITVoyagers

Bitwise Operators

bitwise-operator-itvoyagers

Bitwise operations like finding one’s complement, shifting bits, AND, OR and XOR are performed using these operators.

One’s Complement “~”

It will returns one’s complement of value.                                               

Syntax :

  ~operand

Example :

  unsigned char a = 8;

  printf(“%d”,~a);

Output :

  -9

 

Left shift “<<“

It will shift all bits to left. We have specify number of positions we want to shift bits. When we shift all bits by one position then variable’s value will get double.

Syntax :

  Operand<<Number_of_position;;

Example :

    unsigned char a = 8;  // a = 8 = 00001000

    printf(“%d”,a<<1); 

Output :

  16    // a = 16 = 00010000

 

Right shift “>>”

It will shift all bits to right. We have specify number of positions we want to shift bits. When we shift all bits by one position then variable’s value will get divide by 2.

Syntax :

  Operand>>Number_of_position;

Example :

    unsigned char a = 8; // a = 8 = 00001000

    printf(“%d”,a>>1);

Output :

  4      // a = 4 = 00000100

 

 

Bitwise AND “&”

It will perform AND operation on two value and returns a result.                                         

Syntax :

  Operand_1 & Operand_2

Example :

  // 8 = 00001000 & 9 = 00001001

  unsigned char a = 8, b=9, c;

  // 00001000 AND 00001001

  printf(“%d”, a & b);

Output :

  8  // c = 00001000

 

Bitwise OR “|”

It will perform OR operation on two value and returns a result.                                         

Syntax :

  Operand_1 | Operand_2

Example :

  // 8 = 00001000 & 9 = 00001001

  unsigned char a = 8, b=9, c;

  // 00001000 OR 00001001

  printf(“%d”, a | b);

Output :

  8  // c = 00001001

 

Right shift “>>”

It will perform XOR operation on two value and returns a result.                                         

Syntax :

  Operand_1 ^ Operand_2

Example :

  // 8 = 00001000 & 9 = 00001001

  unsigned char a = 8, b=9, c;

  // 00001000 OR 00001001

  printf(“%d”, a ^ b);

Output :

  1  // c = 00000001

 

Precedence and Associativity off Operators in C

PrecedenceOperatorsDescriptionAssociativity
1( )
[ ]
.
->
++ —
Parentheses or Function call
Bracket or Array subscript
Dot or member selection operator
Arrow operator
Postfix increment/decrement
Left-To-Right
2++ —
+ –
! ~
(type)
*
&
sizeof
Prefix increment/Decrement
Unary plus and minus
NOT and Bitwise Complement
Type cast
Indirecion or dereference operator
Address of operator
Determine size in bytes
Right-To-Left
3* / % Multiplication, division and Modulus Left-To-Right
4+ – Addition and Subtraction Left-To-Right
5 >> << Bitwise Right shift and Bitwise Left Shift Left-To-Right
6 < <=
> >=
Relation less than/ less than equal to
Relation greater than/greater than equal to
Left-To-Right
7== !=Relation equal to and not equal to Left-To-Right
8&Bitwise AND Left-To-Right
9^Bitwise XOR Left-To-Right
10|Bitwise OR Left-To-Right
11&&Logical AND Left-To-Right
12||Logical OR Left-To-Right
13? :Ternary/Conditional operatorRight-To-Left
14=
+= -=
*= /=
%= &=
^= |=
>>= <<=
Assignment operator
Addition and Subtraction assignment
Multiplication and Division assignment
Modulus and Division assignment
Bitwise XOR and OR assignment
Bitwise Right and Left shift assignment
Right-To-Left
15,Comma operator Left-To-Right

Try this fun game

CHECKOUT OTHER RELATED TOPICS

PRACTICALS IN C PROGRAM

We are aiming to explain all concepts of C in easiest terms as possible

Leave a Comment