Operators in C Programming With Examples

In this post, we will learn about operators in c. We will go through operand, operators  and classification of operator such as unary operator, binary operator, ternary operator. We will discuss all operators with appropriate example that comes under the category of unary operators, binary operators and ternary operator. For example, Logical NOT operator is a Logical operator that comes under unary operator while other Logical operator such as Logical OR and Logical AND is binary operator. So, we will discuss all according to their category.

1. Operand

  • An operand is a term used to describe any object that is capable of being manipulated.
  • An operand may be any numerical value, constant, variables on which program makes an operation.

For example,

In the expression 20 + 40, 20 and 40 are operands and ‘+’ is an operator.

In the expression z = x – y, x, y and z are operands while ‘-‘ and ‘=’ are operators.

In the expression y = x + 5, x, y and 5 are operands while ‘+’ and ‘=’ are operators.

2. Operator

  • An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations.
  • Operators are used in programs to manipulate data and variables.
  • C supports a rich set of operators.

In C programming language, unary, binary, ternary operators are useful for carrying out mathematical operations. Some of the operators are –

Tutorialwing C Operators Example Unary, Binary and Ternary

As depicted in the image, there are three categories of operators –

  1. Unary Operator
  2. Binary Operator
  3. Ternary Operator

Let’s go through each operator one by one.

2.1 Unary Operators in C

  • Unary operators are those operators that perform operation on only one operand.
  • For example, increment operator (++) , decrement operator (- -).

Example –

int a = 5;
a++; // Here a is an operand and ++ is a increment operator that increase the value of operand by one.

Unary Operators in C –

Operator Symbol Operator Name
! Logical Negation (NOT)
~ One’s Complement
+ Unary Plus
Unary Minus
++ Increment Operator
– – Decrement Operator
* Dereferencing Operator
& Address of Operator
sizeof() size of operator
(type) type cast (conversion)

Let’s go through each unary operators in c one by one.

2.1.1. (NOT) (!) Logical Negation Operator in C

It is a Logical operator and used to make decisions. It inverses the result of any expression. If result is non zero (i.e. true) it returns zero (0, i.e. false) and if result is zero (0, i.e. false) it returns 1 (i.e. true).

Truth Table of Logical Negation –

Expression Logical Negation (NOT) (!)
0 1
1 0

Program to show the use of Logical NOT operator:

#include<stdio.h>
int main()
{
	int a=10;
	printf("a= %d", !(a==8));
	printf("a= %d", !(a==10));
	return 0;
}

Output:

a= 1
a= 0

2.1.2. (~) One’s Complement Operator in C

It is also known as Bitwise NOT Operator. It inverses every bits of operand from 0 to 1 and 1 to 0.

Program to show the use of One’s Complement operator:

#include<stdio.h>
int main() 
{ 
    unsigned char a = 10; // a = 10(00001010)
    printf("a = %d\n", a); 
    a = ~a;
    printf("~a = %d\n", a);
}

Output:

a = 10 //00001010
a = 245 //11110101

2.1.3. (+) Unary Plus Operator in C

It doesn’t alter the value or sign of its operand. The result of the unary + operator is the value of its operand.

Program to show the use of Unary Plus Operator:

#include<stdio.h>
int main()
{
	int a= +10;      
	printf("a= %d\n",a);
	return 0;	
}

Output:

a= 10

2.1.4. (-) Unary Minus Operator in C

It negates the value of operand. It makes positive value to negative and negative value to positive value.

Program to show the use of Unary Minus Operator:

#include<stdio.h>
int main()
{
	int a= -15;
	int b= 15;
	printf("value of a: %d\n",-a);
	printf("value of b: %d\n",-b);
	return 0;	
}

Output:

Value of a: 15
Value of b: -15




2.1.5. (++) Increment Operator in C

++ Operator adds 1 to the operand.

Increment can be done using two methods –

Let’s go through each method one by one –

A. Pre Increment Operator

Pre means before. So, the variable value is incremented first, then used in the expression.

Program to show the use of Pre-Increment Operator:

#include<stdio.h>
int main()
{
	int a = 10;
	int b = 20;  
	printf("value of a: %d\n",++a);
	printf("value of b: %d\n",++b);
	printf("value of ++a + ++b + ++a expression :%d", ++a + ++b + ++a);
	return 0;	
}

Output:

value of a: 11
value of b: 21
value of ++a + ++b + ++a expression : 47

B. Post Increment Operator

Post means after. So, increment is done after the variable is used in expression.

Program to show the use of Post-Increment Operator:

#include<stdio.h>
int main()
{
	int a = 10;
	int b = 20; 
	printf("value of a: %d\n",a++);
	printf("value of b: %d\n",b++);
	printf("value of a++ + b++ + a++ expression :%d", a++ + b++ + a++);
	return 0; 
}

Output:

value of a: 10
value of b: 20
value of a++ + b++ + a++ expression : 44

2.1.6. (- -) Decrement Operator in C

Decrement Operator (- -) subtracts 1 from the operand.

Decrement can be done using two methods –

Let’s go through each method one by one –

A. Pre Decrement Operator in C

Pre means before. So, the variable value is decremented first, then used in the expression.

Program to show the use of Pre-Decrement Operator:

#include<stdio.h>
int main()
{
	int a = 10;
	int b = 20;  
	printf("value of a: %d\n",--a);
	printf("value of b: %d\n",--b);
	printf("value of (--a + --b + --a) expression: %d", --a + --b + --a);
	return 0;	
}

Output:

value of a: 9
value of b: 19
value of ( – – a + – – b + – – a) expression : 33

B. Post Decrement Operator in C

Post means after. So, the decrement is done after the variable is used in the expression.

Program to show the use of Post-Decrement Operator:

#include<stdio.h>
int main()
{
	int a = 10;
	int b = 20; 
	printf("value of a: %d\n",a--);
	printf("value of b: %d\n",b--);
	printf("value of a-- + b-- + a-- expression :%d", a-- + b-- + a--);
	return 0; 
}

Output:

value of a: 10
value of b: 20
value of a – – + b – – + a – – expression : 36

2.1.7. (*) Dereferencing Operator in C

It is used to deference the value of any pointer variable.

Program to show the use of Dereferencing Operator:

#include<stdio.h>
int main()
{
	int a = 10;  // Here, a is a variable that holds  value 10.
	int *ptr;   // Here , ptr is a pointer variable that holds the address of a variable
	ptr = &a;  // Here, ptr holds the address of variable a 
	printf("Value of a is: %d\n",*ptr); // To access the value of a, we use * dereferencing operator before ptr
	return 0;
}

Output:

Value of a is 10

2.1.7. (&) Address of Operator in C

This operator returns address of any variable.

Program to show the use of Address of Operator:

#include<stdio.h>
int main()
{
	int a=10; // Here, a is a variable that holds value 10.
	int *ptr; // Here , ptr is a pointer variable that holds the address of a variable
	ptr = &a; // Here, ptr holds the address of variable a
	printf("Value of a: %d\n",a);
	printf("Address of a: %X\n",&a);
	printf("Value of ptr is: %X\n",ptr);  // Ptr holds address of variable a 
	printf("Address of ptr: %X\n",&ptr);  
	return 0;
}

Output:

Value of a: 10
Address of a:23FE43
Value of ptr: 23FE43
Address of ptr:23FE40

2.1.8. sizeof() Operator in C

  • It is a compile time Operator.
  • It returns the number of bytes the operand occupies.
  • The operand may be a variable, expression, a constant or a data type qualifier.
  • used to determine the length of arrays and structure when their size are not known to the programmer.

Program to show the use of sizeof() operator:

#include<sdtio.h>
int main()
{
	int a=10;
	printf("size of int  : %d\n",sizeof(int));
	printf("size of char : %d\n",sizeof(char));
	printf("size of float: %d\n",sizeof(float));
	printf("size of double: %d\n",sizeof(double));
	printf("size of a    : %d\n",sizeof(a));
	printf("size of 'b'   : %d\n",sizeof('b'));
	printf("size of 235L   : %d\n",sizeof(235L));
	printf("size of 2.45   : %d\n",sizeof(2.45));
	return 0;
}

Output:

size of int : 4
size of char :1
size of float: 4
size of double:8
size of a: 4
size of ‘b’: 1
size of 235L: 4
size of 2.45: 8

2.1.9. Typecast or type conversion in C

Type casting refers to changing a variable of one data type into another data type.

Program to show the use of Typecast or type conversion:

#include<stdio.h>
int main ()
{
float x,y;
x = (float)7/5;
y=7/5;
printf(" x is %f\n",x);
printf(" y is %f",y);
return 0;
}

Output:

x is 1.400000
y is 1.000000

2.2 Binary Operators in C

  • Binary Operators are those operators that perform operations on two operands.
  • For example: ‘+’,  ‘-‘, ‘/’, ‘*’.

Let’s take another example –

a + b; // Here a and b are operands while ‘+’ is an operator that performs plus operation on a and b.

Some of the Binary Operators in C are listed below –

Type Operators Symbols
Arithmetic Operators +, -, *, /, %
Relational Operators <, <= , > , <= , == , !=
Logical Operators  && , ||, !
Bitwise Operators &, |, ^, <<, >>, ~
Assignment Operators  = , += , -=, *=, /=, %=



2.2.1 Arithmetic Operators in C

  • It is used to perform mathematical operations on operand.
  • It can operate on any built-in data type allowed in C.

Below are some of the arithmetic operators in C –
Let’s assume a = 20 and b = 10.

Operator Used for Example
+ Addition:

  • adds two operand
a + b = 30
Subtraction:

  • subtracts two operand
a – b = 10
* Multiplication:

  • multiplies two operand
a * b = 200
/ Division:

  • It returns quotient when first operand is divided by second operand.
a / b = 2
% Modulo Division:

  • It returns remainder when first operand is divided by second operand.
  • The modulo Division operator % can’t be used on floating point data.
a % b = 0
b % a = 1

On the basis of Operations, Arithmetic operations are of three types:

A. Integer Arithmetic Operation

When both the operands in a single arithmetic expression such as a + b are integers, the expression is called an integer expression and the operation is called Integer Arithmetic Operation.

Example –

Assume a and b are integers where a = 10, b = 5. Then, we have following results:
a + b = 15
a – b = 5
a * b = 50
a / b = 2 (Decimal part truncated)
a % b = 0 (Remainder of division)
b % a = 5 (Remainder of division)

During Integer Division, if both the operands are of the same sign, the result is truncated towards zero. If one of them is negative, the direction of truncation is implementation dependent.

Example –

6 / 7 = 0
-6 / -7 = 0
-6 / 7 may be 0 or -1 (Machine dependent)

During Modulo Division, the sign of the result is always the sign of the first operand (the dividend).

Example –

-14 % 3 = -2
-14 % -3 = -2
14 % -3 = 2

Program to show the use of Integer Arithmetic to convert days to Months and days:

#include<stdio.h>
int main()
{
	int months, days;
	printf("Enter Days\n");
	scanf("%d", &days);
	months = days / 30;
	days = days % 30;
	printf(" Months = %d \t Days = %d \t", months, days);
	return 0;
}

Output:

Enter Days
265
Months = 8 Days = 25

B. Real Arithmetic Operation

An Arithmetic operation involving only real number operands is called real arithmetic operation.

Example –

if x and y are floats, then
x=6.0/7.0 = 0.857143
y=-2.0/3.0= -0.666667

C. Mixed Mode Arithmetic Operation

  • When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression.
  • If either operand is of the real type, then only the real arithmetic operation is performed and the result is always a real number.

Example –

15/10.0 = 1.5
where as 15/10 =1

Program to show the use of Arithmetic Operators:

#include<stdio.h>
int main()
{
	int a, b;
	printf("Enter the value of a and b\n");
	scanf("%d %d", &a, &b);
	printf("\n Addition\n");
	printf("%d + %d is %d",a,b,a+b);
	printf("\n Subtraction\n");
	printf("%d - %d is %d",a,b,a-b);
	printf("\n Multiplication\n");
	printf("%d * %d is %d",a,b,a*b);
	printf("\nDivision\n");
	printf("%d / %d is %d",a,b,a/b);
	printf("\n Modulo Division\n");
	printf("%d mod %d is %d and %d mod %d is %d",a,b,a%b,b,a,b%a);
	return 0;
}

Output:

Enter the value of a and b
14
4
Addition
14 + 4 is 18
Subtraction
14 – 4 is 10
Multiplication
14 * 4 is 56
Division
14 / 4 is 3
Modulo Division
14 % 4 is 2 and 4 % 14 is 4

2.2.2 Relational Operators in c

  • It is used to compare two quantities and depending on their relations, take certain decisions.
  • For Example: can used to compare marks , price of two items ,etc.
  • It is used in decision statements such as, if and while to decide the course of action of a running program.
  • An expression such as  a < b or 1 < 20 containing a relational operator is termed as a relational expression.
  • The value of a relational expression is either 1 or 0, If the specified relation is true the then value will be one(1), and zero (0) if the relation is false.

Example:

0 < 20 is true (1) 20 < 0 is false (0)

 C supports 6 Relational Operators :

Operator Meaning Example
<  is less than  2 < 4  is true (1)
<=  is less than or equal to  3 <=3 is true (1)
>  is greater than  5 > 4 is true (1)
>=  is greater than or equal to  1 >= 4 is false (0)
==  is equal to  2 == 3 is false (0)
 != is  not equal to 2 != 1 is true (1)

Program to show the concept of Relational Operators:

#include <stdio.h> 
int main() 
{ 
    int a = 15, b = 50, c = 11; 
    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d == %d is %d \n", a, c, a == c);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d > %d is %d \n", a, c, a > c);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d < %d is %d \n", a, c, a < c);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d != %d is %d \n", a, c, a != c);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= c);
    printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= c);
    return 0;
} 

Output:

15 == 50 is 0
15 == 11 is 0
15 > 50 is 0
15 > 11 is 1
15 < 50 is 1 15 < 11 is 0 15 != 50 is 1 15 != 11 is 1 15 >= 50 is 0
15 >= 11 is 1
15 <= 50 is 1 15 <= 11 is 0

2.2.3 Logical Operators in C

  • It is used to combine two or more conditions.
  • It is used to perform logical operations on the given expressions.
  • The logical operators are used when we want to test more than one condition and make decision. That’s why Logical AND and Logical OR are called Binary operators.
  • Logical NOT is used to test only one condition. So, Logical NOT is called unary operator.
    Example:

    if a > b && x == 10 is true when a > b is true and x == 10 is true. If one of them is false, then expression is false.

An expression of this kind which combines two or more relational expressions is termed as a logical expression.

C supports 3 logical Operators:

Assume a = 10 , b = 20, c = 30.

Operators Meaning Example
 &&  Logical AND:
If both the operands are non-zero (i.e. true), then the condition becomes true.
 a == b  &&  c > b

Here a==b  i.e. 10 == 20 is false ,

c > b  i.e. 30 > 20 is true

But result will be false because both expressions are not true.

 ||  Logical OR:

If any of the two operands are non-zero, then the condition becomes true.

(a == b && c > b) || (a < b) Here a == b  i.e. 10 == 20 is false(0) , c > b  i.e. 30 > 20 is true (1)

a < b i.e. 10 < 20 is true(1) So, (0 && 1) || 1 0 || 1 Result will be 1 (true).

!  Logical NOT:

It inverse the result of any expression, if  result is non zero it returns zero (0) and if result is zero its returns 1.

 !(a == b && c > b) && (a < b) Here a == b  i.e. 10 == 20 is false(0) , c > b  i.e. 30 > 20 is true (1)

a < b i.e. 10 < 20 is true(1) So,  !  (0 && 1) &&  1 !( 0) & 1 1 && 1 1 So , Result will be True (1).

Truth Table of Logical AND and Logical OR Operator:

Operand 1 Operand 2  Operand 1 && Operand 2  Operand 1 || Operand 2
 Non-zero  Non-zero 1 1
 Non-zero 0 0 1
0  Non-zero 0 1
0 0 0 0

Program to show the concept of Logical Operators:

#include<stdio.h>
int main() 
{ 
   int a=10, b=20, c=30; 
   // logical AND Operator
    printf("%d\n",(a<b && c==b));
   // logical OR Operator 
    printf("%d\n",(a<b || c==b)); 
   // logical NOT Operator 
    printf("%d\n",!a) ;
    return 0; 
}

Output:

0
1
0




2.2.4 Bitwise Operators in C

  • It is used for manipulation of data at bit level.
  • It is used for testing the bits , or shifting them right to left.
  • It may not be applied to float or double.

C supports 6 Bitwise Operators:

Assuming a = 10, b = 20. So,
10 = 00001010 (In Binary)
20 = 00010100 (In Binary)

 Operator  Meaning / Explanation  Example
 &  Bitwise AND:
It takes two operands and does AND on every bit of two operands. The result of AND is 1 only if both bits are 1.
Bit Operation of 10 and 20

   00001010
& 00010100
——————————
   00000000 = 0 (In Decimal)

|  Bitwise OR:
It takes two operands and does OR on every bit of two operands. The result of OR is 1 if any of the two bits is 1.
Bit Operation of 10 and 20

  00001010
| 00010100
——————————
  00011110 = 30 (In Decimal)
 ^  Bitwise exclusive OR (XOR):
It takes two operands and does XOR on every bit of two operands. The result of XOR is 1 if the two bits are different.
Bit Operation of 10 and 20

   00001010
^ 00010100
——————————
   00011110 = 30 (In Decimal)
 <<  Shift left:
It takes two operands, left shifts the bits of the first operand, the second operand decides the number of places to shift.
 
10 = 00001010 (In binary)

10 << 2 = 00101000 = 40 (in decimal) (left Shift by 2 bits)
 >>  Shift Right:
It takes two operands, right shifts the bits of the first operand, the second operand decides the number of places to shift.
10 = 00001010 (In binary)

10 >> 2 = 00000010 = 2 (in decimal)
(Right Shift by 2 bits)

 ~  One’s Complement:
It takes one operands and inverts all bits of it.
10 = 00001010
~10 = 1 1 1 10101 = 245 (in decimal)
The 2’s complement of 245 is -11. Hence, the output is -11 instead of 245.

The left shift and right shift operators should not be used for negative numbers.

Program to show the concept of Bitwise Operators in C:

#include<stdio.h>
int main() 
{ 
    int a = 10, b = 20; 
    printf("a = %d, 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("~a = %d\n", a = ~a);   
    printf("b<<1 = %d\n", b<<1);   
    printf("b>>1 = %d\n", b>>1); 
    return 0; 
} 

Output:

a=10 , b=20
a & b = 0
a|b = 30
a^b = 30
~a = -11
b<<1 = 40 b>>1 = 10

2.2.5 Assignment Operators in C

  • It is used to assign the result of an expression to a variable.
  • We have seen the usual assignment operator (= ). In addition, C has a set of shorthand assignment operators of the form.

Syntax of Assignment Operator: 

v op= exp;
where v for variable,
op for arithmetic_operator
= is assignment operator
exp for expression
The operator op= is known as the shorthand assignment operator.

Shorthand Assignment Operators in C:

Statement with simple Assignment Operator  Statement with Shorthand Assignment Operator
a = a + 1  a += 1
 a = a – 1  a -= 1
 a = a * ( n + 1 )  a *= (n + 1)
 a = a / ( n + 1)  a /= (n + 1)
 a = a % b a %= b

Example:

x + = y+1;
This is same as the statement
x = x + (y + 1);

Program to show  the concept of Shorthand Assignment Operator:

#include<stdio.h>
int main()
{
   int a = 10, result;
   printf("Simple Assignment Operator result= %d\n", result = a);
   printf("Shorthand Addition Operator result= %d\n", result += a);
   printf("Shorthand Subtraction Operator result= %d\n", result -= a);
   printf("Shorthand Multiplication Operator result= %d\n", result *= a);
   printf("Shorthand Division Operator result= %d\n", result /= a);
   printf("Shorthand Modulo Division Operator result= %d\n", result %= a);
   return 0;
}

Output:

Simple Assignment Operator result= 10
Shorthand Addition Operator result= 20
Shorthand Subtraction Operator result= 10
Shorthand Multiplication Operator result= 100
Shorthand Division Operator result= 10
Shorthand Modulo Division Operator result= 0

2.3 Ternary Operator in C

Ternary Operator are those that  perform operations on three operands.

Syntax of ternary operator

conditional expression ? expression1 : expression2

In this syntax, the conditional expression is evaluated first. If the result is non-zero (i.e. true), expression1 is evaluated and result is returned as the value of the conditional expression. Otherwise , expression2 is evaluated and result is returned as the value of the conditional expression. That’s why this operator is called Conditional Operator.

For example,

a ? b : c

In this example, a is condition, and b and c will be either value, variable, statement or any mathematical expression. If condition will be true then b will be executed otherwise c will be executed.

Operator Name Operator Symbol Example
Conditional Operator or Ternary Operator  ? : int x = 20, y = 30;
int max;
max = (x > y) ? x : y;

Program to show the use of ternary operator:

 
#include <stdio.h> 
int main() 
{ 
    int a; 
    a = 25 > 2 ? 1 : 0;
    printf("\nValue of a:%d", a); 
    a = 12 > 10 != 11 ? 25 < 38 && 38 > 52 :2;
    printf("\nValue of a:%d", a);
	a = 15 < 2 ? 15 : 2;
    printf("\nValue of a:%d", a);  
    return 0; 
} 

Output:

Value of a = 1
Value of a = 0
Value of a = 2

Conclusion

Thus, we have seen different operators in c – Unary operators, binary operators and ternary operators. We have also gone through different examples to make the concepts more clear. Let me know in the comment section below if you have any question. Now, you can can checkout different storage classes in C.

Leave a Reply