Operator in C

Operator in C 

In C Programming language, operator are symbols that represent Operation to be perform on one or more operands. An operators specified an operation to be performed that yields a value.

Here are some commonly used operator  in C :

1. Arithmetic Operators

Arithmetic operators are used for numeric calculations. These  Operators are used to perform for mathematical/arithmetic operations on operands. They are of two types-

a. Unary Arithmetic operators

unary operators require only one operand. for example:
    +x        -y
here '-' changes the sign of the operand y.

b. Binary Arithmetic operators
Binary operators require two operands. There are five arithmetic operators-
Operator Purpose
+ addition
- subtraction
* multiplication
% Gives the remainder in integer division

NOTE : %(modulus operator) cannot be applied with floating point operands. There is no exponent operator in C, however there is a library function pow() to carry out exponentiation operation. 

Program to demonstrate arithmetic operator:

#include<stdio.h>
int main()
{
    int a=17,b=4;
    printf("Sum = %d\n",a+b);
    printf("Subtraction = %d\n",a-b);
    printf("Multiplication = %d\n",a*b);
    printf("Division = %d\n",a/b);
    printf("Remainder=%d\n",a%b);
    return 0;
}

Output :
 
Sum = 21
Subtraction = 13
Multiplication = 68
Division = 4
Remainder=1

2. Assignment Operators

Assignment operator are used to assign the value to variable. The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. some assignment operators are (=, +=, -=, *=, /=)

x = 8   /* 8 is assigned to x*/

#include<stdio.h>
int main()
{
    int a=17,b=4; /* 17 assign to a and 4 assign to b*/
    a-=1; // a-=1 is equivalent to a=a-1
    b+=5; // b+=5 is equivalent to b=b+5
    printf("a=%d\n",a);
    printf("b=%d",b);
    return 0;
}

Output :
 a=16
b=9

 3. Increment and Decrement  Operators

 The increment(++) and decrement(--) operators are unary operators because they operate on a single operand. The increment operator increments the value by 1, while decrement operator decrements the value by 1.

++x    is equivalent     x=x+1

--x     is equivalent     x=x-1

Note : ++5 or ++(x+y) are Invalid

These operators are of two types -

1.prefix increment/decrement-operator is written before the operand(e.g. ++x,--x) 
2. postfix increment/decrement-operator is written after the operand(e.g. x++,x--)

program to demonstrate prefix increment and decrement operators:

#include <stdio.h>
int main()
{
    int a = 9;
    printf("a=%d\t", a);
    printf("a=%d\t", ++a);/*prefix increment*/
    printf("a=%d\t", a);
    printf("a=%d\t", --a);/*prefix decrement*/
    printf("a=%d\t", a);
    return 0;
}

Output :
 a=9     a=10     a=10    a=9    a=9 

program to demonstrate postfix increment and decrement operators:

#include <stdio.h>
int main()
{
    int a = 9;
    printf("a=%d\t", a);
    printf("a=%d\t", a++);/*postfix increment*/
    printf("a=%d\t", a);
    printf("a=%d\t", a--);/*postfix decrement*/
    printf("a=%d\t", a);
    return 0;
}

Output :
 a=9     a=9     a=10    a=10    a=9 

 4. Relational  Operators

 Relational operators are used to compare values of two expression. If the relation is true then the value of relational expression is 1 and if the relation is false then the value of expression is 0. The relational operators are -

operatorMeaning
< less than
<= less than equal to
== equal to 
!= not equal to
> greater than
>= greater than or equal to

program to demonstrate relational operator :

#include <stdio.h>
int main()
{
    int a, b;
    printf("Enter the value of a and b:\n");
    scanf("%d%d", &a, &b);
    if (a < b)
        printf("%d is less than %d", a, b);
    if (a <= b)
        printf("%d is less than or equal to %d", a, b);
    if (a == b)
        printf("%d is equal to %d", a, b);
    if (a != b)
        printf("%d is not equal to %d", a, b);
    if (a > b)
        printf("%d is greater than %d", a, b);
    if (a >= b)
        printf("%d is greater than or equal to %d", a, b);
    return 0;
}

output :
Enter the value of a and b:
6
8
6 is less than 8
6 is less than or equal to 8
6 is not equal to 8

NOTE : "=" is simplest assignment operator and "==" is equal to operator.

 5. Logical  Operators

An expression that combines two or more expressions is termed as a logical expression. The result of the operation of logical operator is a boolean value either true or false.

operatorName
&& AND
|| OR
!NOT



# AND Operator (&&)

This operator gives the net result true if both the condition are true, otherwise the result is false.
Boolean Table
condition 1condition 2Result
FalseFalseFalse
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

# OR Operator (||)

This operator gives the net result false, if both the condition have the value false, otherwise the result is true.
Boolean Table
condition 1condition 2Result
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueTrue

#NOT Operator (!)

This Unary operator and it negate the value of the condition. If the value of the condition is false then it gives the result true. If the value of the condition is true then it gives the result false.
Boolean Table
condition 
Result
FalseTrue
True
False

Program to demonstrate Logical operators

#include <stdio.h>
int main()
{
    int a, b;
    printf("Enter the value of a and b:\n");
    scanf("%d%d", &a, &b);
    printf("a && b = %d\n",a&&b);
    printf("a || b = %d\n",a||b);
    printf("!a = %d\n",!a);
    return 0;
}

Output:
Enter the value of a and b:
0
2
a && b = 0
a || b = 1
!a = 1

 6. Conditional  Operators

Conditional operator are also called Ternary operator (? and :) which required three expression as operands. It is written as -

syntax:
TestExpression ? expression1 : expression2 ;

If test expression is true(non-zero), then expression1 is evaluated otherwise test expression is false(zero), then expression2 is evaluated.

We may replace the use of if..else statements with conditional operators.

Program to demonstrate Logical operators

#include <stdio.h>
int main()
{
    int a, b,max;
    printf("Enter the value of a and b:\n");
    scanf("%d%d", &a, &b);
    max=a>b?a:b;
    printf("larger of %d and %d is %d\n",a,b,max);
    return 0;
}

Output:
Enter the value of a and b:
25
23
larger of 25 and 23 is 25

7. Bitwise  Operators

C has the ability to support manipulation of data at the bit level. Bitwise operators operate on integers only and they are used for operations on individual bits. The bitwise operators are:

Bitwise operatorMeaning
&bitwise AND
|bitwise OR
~One's complement
<<left shift
>>right shift
^bitwise XOR

/*Program to demonstrate Bitwise operators*/
#include <stdio.h>
int main()
{
    int a=5/*(00000101)*/, b=9/*(00001001)*/;
    printf("The value of a =%d and b=%d\n",a,b);
    printf("The value of a&b is %d\n",a&b);
    printf("The value of a|b is %d\n",a|b);
    printf("The value of a^b is %d\n",a^b);
    printf("The value of a>>1 is %d\n",a>>1);
    printf("The value of a<<1 is %d\n",a<<1);
    printf("The value of ~a is %d\n",~a);
    return 0;
}

Output:
The value of a =5 and b=9
The value of a&b is 1
The value of a|b is 13
The value of a^b is 12
The value of a>>1 is 2
The value of a<<1 is 10
The value of ~a is -6

Thank you

Comments

Popular posts from this blog

18 Amazing programs in C.