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
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
// 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));
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 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
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
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
Comments
Post a Comment