Operators
Arithmetic Operator
//Arithmetic operators
#include < stdio.h >
int main()
{
int a = 2, b = 3;
int sum, sub, mul, rem, idiv ;
float rdiv;
sum = a + b;
sub = a – b;
mul = a * b;
idiv = a / b;
rem = a % b;
rdiv = (float)a / (float)b;
printf( “\n sum = %d”, sum );
printf( “\n sub = %d”, sub );
printf( “\n mul = %d”, mul );
printf( “\n idiv = %d”, idiv );
printf( “\n rem = %d”, rem );
printf( “\n rdiv = %f”, rdiv );
return 0;
}
Output:
sum = 5
sub = -1
mul = 6
idiv = 0
rem = 2
rdiv = 0.666667
Relational Operators
//Relational Operators
#include < stdio.h >
int main()
{
int n;
printf( “\n Enter any Number : ” );
scanf( “%d”, &n );
printf( “\n (n < 10) is = %d”, (n 10) is = %d”, (n > 10) );
printf( “\n (n <= 10) is = %d”, (n = 10) is = %d”, (n >= 10) );
printf( “\n (n == 10) is = %d”, (n == 10) );
printf( “\n (n != 10) is = %d”, (n != 10) );
return 0;
}
Output:
(n 10) is = 0
(n = 10) is = 1
(n == 10) is = 1
(n != 10) is = 0
Logical (&&) And Operators
// Logical AND ( && ) operator
#include < stdio.h >
Int main ()
{
int a;
a = (2<1) && (4<3);
printf ( “\n (2<1) && (4<3) = %d”, a ) ;
a = (23);
printf ( “\n (23) = %d”, a ) ;
a = (2>1) && (41) && (41) && (4>3);
printf ( “\n (2>1) && (4>3) = %d”, a ) ;
return 0;
}
Output:
(2<1) && (4<3) = 0
(23) = 0
(2>1) && (41) && (4>3) = 1
Logical OR (||) And Operators
// Logical OR ( || ) operator
#include < stdio.h >
int main( )
{
int a;
a = (2<1) || (4<3);
printf ( “\n (2<1) || (4<3) = %d”, a ) ;
a = (23);
printf ( “\n (23) = %d”, a ) ;
a = (2>1) || (41) || (41) || (4>3);
printf ( “\n (2>1) || (4>3) = %d”, a ) ;
return 0;
}
Output
(2<1) || (4<3) = 0
(23) = 1
(2>1) || (41) || (4>3) = 1
[Program finished]
Logical Not ( ! ) Operators
// Logical NOT ( ! ) operator
#include < stdio.h >
#include
int main()
{
int a;
clrscr();
a = !(5<1);
printf ( “\n !(51);
printf ( “\n !(5>1) = %d”, a ) ;
getch();
return 0;
}
Output:
!(51) = 0
Assignment Operator
//Assignment Operator ( = )
#include < stdio.h >
int main()
{
int a, b, c, d;
a = 10;
b = a;
c = a + b;
d = 10;
d = d + 10;/* equivalent to d += 10; */
printf( “\n a = %d, b = %d”, a, b );
printf( “\n c = %d, d = %d”, c, d );
return 0;
}
Output:
a = 10, b = 10
c = 10, d = 10
Increment operator
//Prefix and Postfix Increment ( ++ ) operator
#include < stdio.h >
int main( )
{
int a = 10, b;
b = ++a;
printf( “\n After Prefix Increment : ” );
printf( ” a = %d and b = %d”, a, b);
a = 10;
b = a++;
printf( “\n After Postfix Increment : ” );
printf( ” a = %d and b = %d”, a, b);
getch();
return 0;
}
Output:
After Prefix Increment : a = 11 and b = 11
After Postfix Increment : a = 11 and b = 10
Decrement Operator
// Prefix and Postfix Decrement ( — ) operator
#include
#include
int main( )
{
int a = 10, b;
clrscr();
b = –a;
printf( “\n After Prefix Decrement : ” );
printf( ” a = %d and b = %d”, a, b );
a = 10;
b = a–;
printf( “\n After Postfix Decrement : ” );
printf( ” a = %d and b = %d”, a, b );
return 0;
}
Output:
After Prefix Decrement : a = 9 and b = 9
After Postfix Decrement : a = 9 and b = 10
Bitwise Operator
// Bitwise Operators
#include
main()
{
int a = 3, b = 6, c;
clrscr();
printf( “\n a = %d”, a );
printf( “\n b = %d”, b );
c = a & b;
printf( “\n a & b = %d”, c );
c = a | b;
printf( “\n a | b = %d”, c );
c = a ^ b;
printf( “\n a ^ b = %d”, c );
c = ~a;
printf( “\n ~a = %d”, c );
c = a << 3;
printf( “\n a <> 2;
printf( “\n b >> 2 = %d”, c );
return 0;
}
Output:
a = 3
b = 6
a & b = 2
a | b = 7
a ^ b = 5
~a = -4
a <> 2 = 1
Explain Expression
// Explain Expression
#include
int main()
{
int w, x, y, z, p;
printf( ” Enter 4 Integers : ” );
scanf( “%d %d %d %d”, &w, &x, &y, &z );
p = (w + x) / (y – z);
printf( “\n Value of P = %d”, p );
return 0;
}
Output:
Enter 4 Integers : 10 20 9 6
Value of P = 10
Operator precedence
// Program to explain Operator Precedence
#include < stdio.h >
int main()
{
float result;
result = 4.0 + 3.0 * 8.0 / 4.0;
printf( “\n%f”, result );
result = 1.0 / 2.0 + 3.0;
printf( “\n%f”, result );
result = (1.0 + 2.0) / 3.0;
printf( “\n%f”, result );
result = (1.0 + 2.0 / 3.0) + 4.0;
printf( “\n%f”, result );
return 0;
}
Output:
10.000000
3.500000
1.000000
5.666667
// Automatic or Implicit Type Conversion
#include < stdio.h >
int main()
{
int a = 10, b = 20;
float c = 10.50;
long d = 20L, e;
e = ((a + c) * d) + b;
printf( “\n Value of e ::: %ld”, e );
return 0;
}
Output:
Value of e ::: 430
// Explicit Conversion or Type Casting.
#include < stdio.h >
int main()
{
int a = 10, b = 20;
float c = 10.50;
long d = 20L, e;
e = ((( (long)a + (long)c ) * d) + (long)b );
printf( ” e = %ld”, e );
return 0;
}
Output:
e = 420
// Type Casting
#include < stdio.h >
int main( )
{
float a ;
int x = 10, y = 3 ;
a = x / y ;
printf ( “\n Value of a (without casting ) = %f”, a ) ;
a = ( float ) x / y ;
printf ( “\n Value of a (with casting ) = %f”, a ) ;
return 0;
}
Output:
Value of a (without casting ) = 3.000000
Value of a (with casting ) = 3.333333