Operators in C Programming

Operators perform operations by taking one or more values to give another value. These operations are performed on variables and values. In this lesson, we will learn about operators in C Programming.

Before moving further, we’ve prepared a video tutorial to understand what operators are in C:

Video: Operators in C Programming

Video: Operators in C Programming (Hindi)

For example:

a + b

The types of operators are explained here with an example, where a = 5, b =10. The following are the operators in C Programming:

  • Arithmetic Operators
  • Assignment Operators
  • Arithmetic Assignment Operators
  • Relational/ Comparison Operators
  • Logical Operators
  • Unary Operators
  • Conditional Operators

C Arithmetic Operators

Arithmetic operators perform arithmetical operations, such as addition, subtraction, division, multiplication, etc.

C Arithmetic Operator

The following example shows how to work with Arithmetic operators in C Programming,

#include <stdio.h>

void main () {

      int a, b, c;
      a = 50;
      b = 100;
      c = 150;

      printf("Studyopedia C Tutorial\n\n");
      printf("Arithmetic Operators:\n");

      printf("\nAddition Arithmetic Operator\n");
      printf("Value of (a + b) is %d",(a+b));

      printf("\n\nSubtract Arithmetic Operator\n");
      printf("Value of (b - a) is %d",(b-a));

      printf("\n\nDivision Arithmetic operator\n");
      printf("Value of (c / a) is %d",(c/a));

      printf("\n\nMultiplication Arithmetic operator\n");
      printf("Value of (a * b) is %d",(a*b));

      printf("\n\nModulus Arithmetic operator\n");
      printf("Value of (c % b) is %d",(c%b));

   getch();
}

Here’s the output,

C Programming Arithmetic Operators

C Assignment Operator

Use the Assignment Operator when you need to assign a value to a variable. Assignment means assigning the value of the right operand(s) to the left operand.

C Assignment Operator

Let us see an example”

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

  a = b
  printf("Value of a = %d",a);
  printf("Value of b = %d",b);
  
  return 0;
}

Output

Value of a = 5
Value of b = 5

C Arithmetic Assignment Operators

Use the Arithmetic Assignment Operator when you need to perform arithmetic operations such as add, subtract, divide, multiply, etc and at the same time assign a value.

C Arithmetic Assignment Operator

The following example shows how to work with Arithmetic Assignment Operators in C Programming,

#include <stdio.h>

void main () {

      int a, b, c, d, e;
      a = 5;
      b = 10;
      c = 15;
      d = 20;
      e = 30;
	  
      printf("Studyopedia C Tutorial\n\n");
      printf("Arithmetic Assignment Operators:\n");
	  
      a += 5; 
      printf("\nAdd AND assignment operator\n");
      printf("Value of a: = %d",a);
	 	  
      b -= 5;
      printf("\n\nSubtract AND assignment operator\n");
      printf("Value of b: = %d",b);
	  
      c *= 5;
      printf("\n\nMultiple AND assignment operator\n");
      printf("Value of c: = %d",c);
	  
      d /= 10;
      printf("\n\nDivide AND assignment operator\n");
      printf("Value of d: = %d",d);
	  
      e %= 9;
      printf("\n\nModulus AND assignment operator\n");
      printf("Value of e: = %d",e);

   getch();
}

Here’s the output,

C Programming Arithmetic Assignment Operators

C Relational/ Comparison Operators

Compare two values with relational operators, which are also known as Comparison Operators.

C Relational or Comparison operator

The following example shows how to work with Relational Operator in C Programming,

#include <stdio.h>

void main () {

    int a, b, c;
    a = 5;
    b = 10;
    c = 15;
    
    printf("Studyopedia C Tutorial");
    printf("\nRelational Operators:\n\n");
	  
    printf("Value of (a == b) is %d", (a == b) );
    printf("\nValue of (a != b) is %d", (a != b) );
    printf("\nValue of (b > c) is %d", (b > c) );
    printf("\nValue of (c < a) is %d", (c < a) );
    printf("\nValue of (c >= b) is %d", (c >= b) );
    printf("\nValue of (b <= c) is %d", (b <= c) );

   getch();
}

Here’s the output,

C Programming Relational Operators

C Logical Operators

Logical operators combine conditional statements. For example, we’re considering Boolean variables a and b,

C Logical Operators

The following example shows how to work with Logical Operators in C Programming,

#include <stdio.h>

void main () {

   int a = 30;
   int b = 75;   
   int c = 0;
   
   printf("Logical Operators\n");
   
   if ( a || b ) {
     printf("TRUE: Logical OR Operator\n" );
   }
   
   if ( a && b ) {
     printf("TRUE: Logical AND Operator\n" );
   }
   
   if (!(c && b)) {
     printf("TRUE: Logical NOT Operator\n" );
   }
   
   getch();
}

Here’s the output,

C Programming Logical Operators

C Unary Operators

Unary operators include pre as well as post-increment and decrement operators,
C Unary Operator

The following example shows how to work with Unary Operators in C Programming,

#include <stdio.h>

void main () {

    int a;
    a = 5;

    printf("Studyopedia C Tutorial\n");
    printf("Unary Operators:\n");

    printf("\nUnary Increment Operator\n");
    printf("Value1: %d",a++);
    printf("\nValue2: %d",++a);

    printf("\n\nUnary Decrement Operator\n");
    printf("Value1: %d",a--);
    printf("\nValue2: %d",--a);

    getch();
}

Here’s the output,

C Programming Unary Increment and Decrement Operators

C  Conditional Operators

The conditional operator evaluates Boolean expressions with three operands. It is also known as the ternary operator. Its role is to assign a value to a variable from two given set options.

Here’s how to use it,

variable  = (expression) ? value1 ( if true) : value2 (if false)

The following example shows how to work with the Conditional Operator in C Programming,

#include <stdio.h>

void main () {

    int a, b;
    a = 5;
	  
    printf("Studyopedia C Tutorial\n");
    printf("Conditional Operator\n");

    b = (a > 2) ? 100: 50;
    printf( "\nValue of b = %d",  b );

    getch();
}

Here’s the output,

C Programming Conditional Operators

Let us see another example:

#include <stdio.h>
 
int main() {
    int a = 5;
    int b = 10;
 
    // Conditional Operator
    int res = (a > b) ? a : b;
 
    printf("Result = %d\n", res);
 
    return 0;
}

Output

Result = 10

In this lesson, we learned about Operators in C Programming and how to work with Arithmetic, Unary, Relational, Arithmetic Assignment, and other operators.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

Variables in C Programming
Decision Making Statements in C
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment