Operators in C

Operators in C Language with Examples

In this article, I am going to discuss Operators in C Language with examples. Please read our previous article, where we discussed the Constants in C program. As part of this article, you will learn what are Operators in C, their type, and when and how to use different types of Operators in the C Language with examples.

C Language Operators:

Operators in C Language are the special kind of symbols that performs certain operations on the data. The collection of operators along with the data or operands is known as expression. C language supports various types of operators but depends on the number of operands, operators are classified into 3 types:

  • Unary Operator
  • Binary Operator
  • Ternary Operator

C Language Operators

When we are evaluating any expression with input data we are passing on it is called operands, which symbol we are using is called operators.

Unary Operators in C Language:

Unary means consisting of a single component or element. A Unary Operator in C is an operator that takes a single operand in an expression or a statement. Here + & – operators will indicate the sign of operand like +5, -3, -45.

Types of Unary Operators are:

Increment operators (++): Example: (++x)
Decrement operators (–): Example: (–x)

Increment Operators in C Language:

Increment Operators is a unary operator. It takes one value at a time. It is classified into two types:

  • Post Increment Operator
  • Pre Increment Operator

Post Increment Operators are the operators that are a suffix to its variable. It uses the value present inside the variable. It increments the value present inside the variable by ‘1’ and updates it.

Syntax: Variable++;
Example: x++;

Pre Increment Operators are the operators which is a prefix to their variable. It increments the value present inside the variable by ‘1’ and updates it then it uses the value present inside the variable.

Syntax: ++Variable;
Example: ++x;

Program:
#include<stdio.h>
int main()
{	
 int x, y, z;
 x=5;
 y=x++;
 printf("x: %d  y: %d", x, y);			
 z=++y;
 printf("y: %d  z:%d", x, y);			
}
Output:

Increment Operators in C Language

Steps to Follow:

First, take the memory block.

Types of Unary Operators in C Language

Write down the expression.
    y=x++;
    z=++y;
If the expression contains the post operator removes the post and writes down the expression again.
     y=x;
Then assign priority.
     y=x;
Finally, update the post values into variables (memory block).

Decrement Operators in C Language:

Decrement Operators is a unary operator. It takes one value at a time. It is classified into two types:

  • Post Decrement Operator
  • Pre Decrement Operator

Post Decrement Operators are the operators that are a suffix to its variable. It uses the value present inside the variable. It decrements the value present inside the variable by ‘1’ and updates it.

Syntax: Variable–;
Example: x–;

Pre Decrement Operators are the operators that are a prefix to their variable. It decrements the value present inside the variable by ‘1’ and updates it then it uses the value present inside the variable.

Syntax: –Variable;
Example: –x;

Example to understand Increment and Decrement Operators in C:
#include<stdio.h>
int main()
{	
 int x, y, z;
 x=5;
 y=x--;
 printf("x: %d  y: %d", x, y);				
 z=--y;
 printf("y: %d  z:%d", x, y);				
}
Output:

Decrement Operators in C Language

Steps to Follow:

First, take the memory block.

Types of Unary Operators in C Language

Write down the expression.
    y=x–;
    z=–y;
If the expression contains a post operator removes the post and writes down the expression again.
    y=x;
Then assign priority.
    y=x;
Finally, update the post values into variables (memory block).

Binary Operators in C Language:

Binary operators are those operators that work with two operands. A Binary Operator in C is an operator that takes two operands in an expression or a statement. Here these + & – operators will not indicate the sign of operand but add or subtract two operands, because these are binary operators like 3+5, 3–5.

Types of Binary Operators are:

  1. Arithmetic operators
  2. Logical operators
  3. Relational operators
  4. Bit-wise operators
  5. Assignment operators
Arithmetic operators in C Language:

These operators are used to perform arithmetic/mathematical operation and give us results accordingly.

  1. + (Plus) – Give sum as a result. Example: 3+2=5
  2. – (Minus) – Give the difference as a result. Example: 4-2=2
  3. * (Asterisk) – Used for multiplication and give the product as a result. Example: 4*2=8
  4. / (Forward slash) – Used for division and give quotient as a result. Example: 4/2=2

% (modulus) – Give the remainder as a result. Example: 7%2=1

Example to understand Arithmetic Operators in C Language
#include<stdio.h>
int main()
{
 int a = 11, b = 4;	 		// Declare and initialize variable a and b
 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);	// because both operands are integer result will be an integer
 printf("a %% b = %d\n", a % b);	// % operator returns the remainder of 11/4 i.e 3
     // Signal to operating system everything works fine
 return 0;
}
Output:

Arithmetic operators in C Language

Logical Operators in C Language:

Logical Operators are operators that determine the relation between 2 or more operands and return a specific output as a result of that relation. They had usually worked with Boolean (true/false) values.

NOT (!) – Used to negate a Boolean statement.
Example: NOT (!)
If A=1 and B=0, It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.
!(A && B) is true.

AND (&&) and OR (||) – Used to combine simple relational statements into more complex Expressions.
Example: AND (&&)
If A=1 and B=0, If both the operands are non-zero, then the condition becomes true.
(A && B) is false.

Example: OR (||)
If A=1 and B=0, If any of the two operands are non-zero, then the condition becomes true.
(A || B) is true.

Example to understand the Logical Operators in C Language
#include <stdio.h>
 main()
 {
  	 int a = 5;
  	 int b = 20;
   	 int c ;
  	 if ( a && b ) 
  	{
     	 	printf("Line 1 - Condition is true\n" );
 	 }
    	if ( a || b ) 
    	{
   	 	printf("Line 2 - Condition is true\n" );
     	}
   	 /* lets change the value of a and b */
   	a = 0;
  	b = 10;
   	if ( a && b )
  	{
      		printf("Line 3 - Condition is true\n" );
  	}
   	else 
  	{
      		printf("Line 3 - Condition is not true\n" );
   	}
   	if ( !(a && b) ) 
  	{
      		printf("Line 4 - Condition is true\n" );
   	}
}
Output:

Logical Operators in C Language

Relational Operators in C Language:

These Operators are used to check the relationship between the two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision-making and loops. Programming language like C which doesn’t support Boolean data type return result as 1 or 0. Here 1->True and 0->False. Following are the different types of relational operators supported in C Programming Language.

  1. Greater than (>) – Returns true when left operand value is greater than right operand value. Example: 5 > 3 is evaluated to 0.
  2. Less than (<) – Returns true when the left operand value is less than the right operand value. Example: 5 < 3 is evaluated to 0.
  3. Greater than or equal to (>=) – Return true when left operand value is greater than or equal to the right operand. Example: 5 >= 3 is evaluated to 0.
  4. Less than or equal to (<=) – Return true when left operand value is less than or equals to the right operand. Example: 5 <= 3 is evaluated to 0.
  5. Equal to (==): – Returns true when left operand value is equal to right operand value. Example: 5 == 3 is evaluated to 0.

Not Equal to (!=): – Return true when left operand value is not equal to right operand. Example: 5 != 3 is evaluated to 0.

Example to Demonstrate the Relational Operators in C Language
#include<stdio.h>
int main()
{
    int x = 12, y = 13;
    printf("x = %d\n", x);
    printf("y = %d\n\n", y);	// Is x is greater than y?
    printf("x > y : %d\n", x > y);  // Is x is greater than or equal to y?
    printf("x >= y : %d\n", x >= y);	// Is x is smaller than y?
    printf("x < y : %d\n", x < y);	// Is x is smaller than or equal to y?
    printf("x <= y : %d\n", x <= y);	// Is x is equal to y?
    printf("x == y : %d\n", x == y);	// Is x is not equal to y?
    printf("x != y : %d\n", x != y);	// Signal to operating system everything works fine
    return 0;
}
Output:

Relational Operators in C Language

Bitwise Operators in C Language:

Bitwise Operators in C Programming Language are used for performing bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits. Bitwise operators always evaluate both operands. Bitwise operators work on bits and perform bit-by-bit operations. The following are the different types of bitwise operators supported in C Programming Langauge.

  1. & (AND): Example: a & b
  2. |(OR): Example: a | b
  3. ^(Exclusive OR (XOR)): Example: a ^ b
  4. ~(One’s Complement (NOT)): Example: ~a
  5. >> (Shift Right): Example: a >> 1
  6. << (Shift Left): Example: a << 1
Example to understand the Bitwise Operators in C Language:
#include<stdio.h>
int main( )
{
 int a=9 , b=65;
 printf("Bitwise AND Operator a & b =%d\n", a & b); 
 printf("Bitwise OR Operator a | b =%d\n", a | b);
 printf("Bitwise XOR Operator a ^ b =%d\n", a ^ b);
 printf("Bitwise NOT Operator ~a =%d\n", ~ a);
 printf("SHIFT RIGHT Operator a >> 1 =%d\n", b >> 1);
 printf("SHIFT LEFT Operator a << 1 =%d\n", a << 1);
 return 0;
}
Output:

Bitwise Operators in C Language

Assignment Operators in C Language:

Assignment Operators in C Language are the operator which is used to assign a new value to a variable. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands. The following are the different types of Assignment Operators supported in C Language.

  1. = : Simple assignment operator. Assigns values from right side operands to left side operands. Example: C = A + B will assign the value of A + B to C
  2. += :Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. Example: C += A is equivalent to C = C + A
  3. -+: Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. Example: C -= A is equivalent to C = C – A
  4. *=: Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. Example: C *= A is equivalent to C = C * A
  5. /= :Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. Example: C /= A is equivalent to C = C / A
  6. %=: Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. Example: C %= A is equivalent to C = C % A
Example to demonstrate Assignment Operators in C Language:
#include<stdio.h>
int main( )
{
 int a=5, c;
 c = a;					// c is 5
 printf(“c = %d \n” , c);
 c += a;					// c is 10
 printf(“c = %d \n” , c);
 c -= a;					// c is 5
 printf(“c = %d \n” , c);
 c *= a;					// c is 25
 printf(“c = %d \n” , c);
 c /= a;					// c is 5
 printf(“c = %d \n” , c);
 c %= a;					// c = 0
 printf(“c = %d \n” , c);
 return 0;
}
Output:

Assignment Operators in C Language

Ternary Operator or Conditional Operator in C Language:

This is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as a shortened way of writing an if-else statement.

?: Ternary or Conditional Operator. Example: (a>b)? c : d ;

(a>b) is a condition where if the condition is true the variable “c” in the operator which acts as an operand will be executed if not Operand “d” will be executed.

Example to Understand Ternary Operator in C Language:
#include <stdio.h>
main()
{
   	int a , b;
 	a = 10;
   	printf( "Value of b is %d\n", (a == 1) ? 20: 30 );
 	printf( "Value of b is %d\n", (a == 10) ? 20: 30 );
}
Output:

Ternary Operator or Conditional Operator in C Language

In the next article, I am going to discuss Unary Operators in C Programming Language with Examples. Here, in this article, I try to explain Operators in C Programming Language with examples and I hope you enjoy this Operators in C Language article. Please give your feedback, suggestions about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *