C Language / Operators in C Language

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators.

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Misc Operators

Arithmetic Operators in C Language
Operator Name Symbol Operator Purpose
addition + addition or unary plus
subtraction - subtraction or unary minus
multiplication * multiplication
division / division
modulo % Gives remainder after division
Increment ++ Pre and post increments used to add 1
decrement -- Pre and post decrements used to add 1

Arithmetic Operators in C Language
Program on Arithmetic operators Output
#include <stdio.h>
void main()
{
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);
printf("Remainder a/b = %d \n", a%b);
}
a+b = 15
a-b = 5
a*b = 50
a/b = 2
Remainder a/b = 0


Program on Increment and Decrement operators Output
#include<stdio.h>
void main()
{
int a =10, b=5;
int c =10, d=5;
printf("PreIncrement ++a = %d \n",++a);
printf("PostIncrement b++ = %d \n",b++);
printf("After PostIncrement b value = %d \n",b);
printf("Predecrement --c = %d \n",--c);
printf("Postdecrement d-- = %d \n",d--);
printf("After Postdecrement d value = %d \n",d);
}
PreIncrement ++a = 11
PostIncrement b++ = 5
After PostIncrement b value = 6
Predecrement --c = 9
Postdecrement d-- = 5
After Postdecrement d value = 4


Relational Operators
Operator Name Symbol Operator Purpose
Equal to == Used to check if both operands are equal.
Greater than > Can check if the first operand is greater than the second.
Less than < Can check if the first operand is lesser than the second.
Not equal to != Can check if both operands are not equal.
Greater than or equal to >= Check if the first operand is greater than or equal to the second.
Less than or equal to <= Check if the first operand is lesser than or equal to the second


Sample Programs
Program Output

#include <stdio.h>
void main()
{
int a = 10, b = 5;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d <= %d is %d \n", a, b, a <= b);
}
10 == 5 is 0
10 > 5 is 1
10 < 5 is 0
10 != 5 is 1
10 >= 5 is 1 10 <= 5 is 0


Logical Operators
Operator Name Symbol Operator Purpose
&& Logical AND If both the operands are non-zero, then the condition becomes true.<
|| Logical OR If any of the two operands is non-zero, then the condition becomes true.
! Logical NOT If a condition is true, then Logical NOT operator will make it false.

Sample Program on logical operators
Program Output

#include <stdio.h>
void main()
{
int a = 1;
int b = 1;

if ( a && b )
printf("And Logical operator\n" );

if ( a || b )
printf("Or Logical operator\n" );

if ( !b )
printf("Not Logical operator\n" );
}

And Logical operator Or Logical operator

Bitwise Operators
Operator Name Symbol Description
Binary AND & It gives 1if it exists in both operands value As 1.
Binary OR | It copies a bit 1 if it exists in either operand.
Binary XOR ^ It copies the bit 1 if it is set in one operand but not both.
Binary Ones Complement ~ It is unary operator and has the effect of 'flipping' bits.
Binary Left Shift <<  It is used to move the bits to left side by a specified number.
Binary Right Shift >>  It is used to move the bits to right side by a specified number.

Example
Operatorwise Example Example
Binary AND (A & B) = 12 i.e., 0000 1100
Binary OR (A | B) = 61 i.e., 0011 1101
Binary XOR (A ^ B) = 49 i.e., 0011 0001
Binary Ones Complement (~A ) = -61 i.e. 1100 0011 in 2's Complement form.
Binary Left Shift A << 2 = 240 i.e., 1111 0000
Binary Right Shift A >> 2 = 15 i.e., 0000 1111

Sample Program on Bitwise operators
Program Output

#include <stdio.h>
void main()
{
int a = 15, b = 20;
printf("And operator = %d\n", a&b);
printf("Or operator = %d\n", a | b);
printf("Exclusive Or operator = %d\n", a ^ b);
printf("Complement operator = %d\n", ~a);
printf("Left Shift operator = %d\n", a << 2);
printf("Right Shift operator = %d\n", a >> 2);
}

And operator = 4
Or operator = 31
Exclusive Or operator = 27
Complement operator = -16
Left Shift operator = 60
Right Shift operator = 3


Explanation
Program: a = 15, b = 20; Output
And operator(a&b) And operator = 4
Or operator(a | b) Or operator = 31                                                                                                             
Exclusive Or operator (a ^ b) Exclusive Or operator = 27                                                                                                    
Complement operator (~a) Complement operator = -16                                                                                                    
Left Shift operator (a << 2) Left Shift operator = 60
Right Shift operator (a >> 2) Right Shift operator = 3

Assignment Operators
Operator Name Symbol Description
Simple assignment operator = Assigns values from right side operands to left side operand
Add AND assignment operator += It adds the right operand to the left operand and assign the result to the left operand.
Subtract AND assignment operator -= It subtracts the right operand from the left operand and assigns the result to the left operand.
Multiply AND assignment operator *= It multiplies the right operand with the left operand and assigns the result to the left operand.
Divide AND assignment operator /= It divides the left operand with the right operand and assigns the result to the left operand.
Modulus AND assignment operator. %= It takes modulus using two operands and assigns the result to the left operand.

Examples
Operator Name Example
Simple assignment operator C = A + B will assign the value of A + B to C
Add AND assignment operator C += A is equivalent to C = C + A
Subtract AND assignment operator C -= A is equivalent to C = C - A
Multiply AND assignment operator C *= A is equivalent to C = C * A
Divide AND assignment operator C /= A is equivalent to C = C / A
Modulus AND assignment operator. C %= A is equivalent to C = C % A

Sample Program on logical operators
Program Output
#include<stdio.h>
void main()
{
int a =10, b= 20;
printf("a = %d \n", a);
printf("b = %d \n", b);
b += a;// b = b+a
printf("b += a = %d \n", b);
b -= a;// b = b-a
printf("b -= a = %d \n", b);

b*= a;// b = b*a
printf("b*= a = %d \n", b);
b/= a;// b = b/a
printf("b/= a = %d \n", b);
b%= a;// b = b%a
printf("b%= a = %d \n", b);
}
a = 10
b = 20
b += a = 30
b -= a = 20
b*= a = 200
b/= a = 20
b%= a = 0

Explanation
Program: a = 10, b = 20; Equivalent Output
b += a b = b+a b=10+20= 30
b -= a b = b-a b=30-10=20
b*= a b = b*a b=20*10=200
b/= a; b = b/a b=200/10=20
b%= a b = b%a b=20%10=0

Misc Operators
Operator Name Symbol Description
Size of variable sizeof() Returns the size of a variable.
Address & Returns the address of a variable.
Pointer * Pointer to a variable.
Ternary ? : Ternary / Conditional operator.

Example for Misc Operators
Operator Name Example
Size of variable sizeof(a), where a is integer, will return 4.
Address &a; returns the actual address of the variable.
Pointer *a;
Ternary If Condition is true ? then value X : otherwise value Y

Sample Program on Misc operators
Program Output

#include <stdio.h>
void main() {
int n1 = 10;
short n2=20;
double n3=30;
int *ptrvar;
printf("n1 variable Size = %d\n", sizeof(n1) );
printf("n2 variable Size = %d\n", sizeof(n2) );
printf("n3 variable Size = %d\n", sizeof(n3) );
/* & and * operators */
ptrvar = &n1;
/* 'ptr' now contains the address of 'a'*/
printf("n1 Value = %d\n", n1);
printf("*ptrvar is %d\n", *ptrvar);
/* Ternary operator */
n1 = 10;
n2 = (n1 == 1) ? 20: 30;
printf( "n2 Value = %d\n", n2 );
}


n1 variable Size = 4
n2 variable Size = 2
n3 variable Size = 8
n1 Value = 10
*ptrvar is 10
n2 Value = 30



Home     Back