Types of operators | #Tut-03

Types of operators In C...

In this  tutorial we will learn about the types of operators present in C programming language with the help of examples.


An operator is a symbol which operates on a variable or value.
C is having wide variety of operators which are as follow.


Arithmetic Operators





some conversions and the precedence of operators..


Code Snippet:

// arithmetic operators
    int a = 10, b = 5;
    printf(" a + b = %d\n", a+b);
    printf(" a - b = %d\n", a-b);
    printf(" a * b = %d\n", a*b);
    printf(" a / b = %d\n", a/b);
    // 5 / 10 = 0.5 integer part is 0.
    printf("a mod b = %d\n", a%b);

Output:

 a + b = 15
 a - b = 5
 a * b = 50
 a / b = 2
 a mod b = 0


Relational Operators




How does the logical operators works..?




Code Snippet :

// relational operators
    int n1=509, n2=509;
    printf("n1 > n2 = %d\n", n1 > n2);
    printf("n1 < n2 = %d\n", n1 < n2);
    printf("n1 >= n2 = %d\n", n1 >= n2);
    printf("n1 == n2 = %d\n", n1 == n2);
   

    // logical operators
    //          0       &&      1           =   0
    printf("\n\n(n1 < n2) && (n1 == n2) = %d\n", (n1 < n2) && (n1 == n2));
    //          0       ||      1           =   1
    printf("(n1 < n2) || (n1 == n2) = %d\n", (n1 < n2) || (n1 == n2));
    printf("(n1 == n2) = %d\n", !(n1 == n2));

OutPut : 

n1 > n2 = 0
n1 < n2 = 0
n1 >= n2 = 1
n1 == n2 = 1

(n1 < n2) && (n1 == n2) = 0
(n1 < n2) || (n1 == n2) = 1
(n1 == n2) = 0


Bitwise Operators





These operators operate on each bit of data.
These operators are used for testing, compiling and shifting bits to the right or left.
Bitwise operatros can operate upon integer and character but not on float and double.




explanation :



Code Snippet :

// bitwise operators
    int x = 2, y=2;
    // x = 0010    0100  --> shifting left
    // x = 0010    0001  --> shifing right
   
    printf("x & y = %d\n", x&y);
    printf("x | y = %d\n", x|y);
    printf("x = %d\n", (x>>1));
    printf("x = %d\n", (x<<1));


Output :

x & y = 2
x | y = 2
x = 1
x = 4


Conditional Expression 



This is the ternary operator, because it
operates on three operands. 
Till now we have seen the operators which was using two operands for performing the operations. 


Code snippet :

#include <stdio.h>

int a = 5, b = 3; // global declaration

int main()
{
    // (a > b) ? a : b;
    // if the condition is true then a is returned else b is returned
    (a > b) ? printf("a is greater") : printf("b is greater");

    return 0;
}

Output :

a is greater


Increment and Decrement Operators 





These are the unary operators which operates on a single operand.






Code Snippet :

#include<stdio.h>

// increment and decrement operator
// 1. post --> value is first asssingned and then increment
// 2. pre --> first increment and then passed to next

int main(){
    int a = 50;

    printf("a = %d\n", a);

    //a++; // increment a by 1 a = a+1; postincrement
    // a la value assingn kara
    // ani then increm.
    ++a;
    printf("a = %d\n", a);

    int b = 12;
    b--; // b = b-1, postdecrement
    printf("b = %d\n", b);
    return 0;
}

Output :

a = 50
a = 51
b = 11


Assignment Operator





Points to remember..!




You can download the notes in pdf format..

Comments

Popular posts from this blog

Introduction to C language | #Tut-01

Loop statements | do-while | while | for | #Tut-05