Loading...
Loading...
00:00:00

What Are Operators?

In C Programming Language, Operator is a symbol that is used to perform some operation. There are multiples operators in C language such as arithmetic, bitwise, conditional, logical Operators etc. here is the some list of operators

  • Arithmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary or Conditional Operators
  • Assignment Operator

Operator's Precedence Rules

Operator's Precedence rules decide the which operator is perform first in a expression. Associavity specifies the Operators direction to be evaluated left to right. If Operator's precedence is same expression must be evaluate from left to right.

Here is the list of Operators precedence rules top to bottom . top mean highest precedence

Operator Description Associativity
()
[]
.
->
Parentheses
subscript
Member selection using object
Member selection using pointer
left-to-right
++ --
+ -
! ~
(type)
*
&
sizeof
Unary preincrement/predecrement
Unary plus/minus
Unary logical negation/bitwise complement
Unary cast (change type)
Dereference
Address
Determine size in bytes
right-to-left
* / % Multiplication/division/modulus left-to-right
+ - Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
< <=
> >=
Relational less than/less than or equal to
Relational greater than/greater than or equal to
left-to-right
==
!=
Relational is equal to
is not equal to
left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
? : Ternary conditional right-to-left
=
+= -=
*= /=
%= &=
^= |=
<<= >>=
Assignment
Addition/subtraction assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment
right-to-left
, Comma (separator) left-to-right

Operators Examples

Arithmetic Operators

Arithmetic Operators are + (plus), - (minus), * (multiply) and / (divide), these are the basic arithmetic Operators.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 20;
    int y = 10;
    printf("The Addition is %d
", x + y);
    printf("The subtraction is %d
", x - y);
    printf("The multiplication is %d
", x * y);
    printf("The division is %d
", x / y);
    printf("The remainder is %d
", x % y);
    return 0;
}

++, -- Increment And Decrement Operators

pre-increment or pre-decrement operator is first change the value of variable then perform the action and post-increment or post-decrement operator is first perform the action then change the value of variables.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 20;
    int y = 10;
    int a = 50;
    int b = 100;
    printf("pre-increment value of x is %d
", ++x);     // 21 
    printf("post-increment value of y is %d
", y++);    //10
    printf("value of y is %d
",y);                      // 11
    printf("pre-decrement value of a is %d
", --a);    // 49
    printf("post-decrement value of b is %d
", b--);   // 100
    printf("value of a is %d
",b);                       // 99
    return 0;
}

Logical Operators

These logical operators are used to determine the logic between variables or values. logical operators always returns true or false (1 or 0) .

operators operations Examples output
&& and 10 > 20 && 10 < 5 false
|| or 10 > 20 || 10 < 5 false
! not !(10 > 20 || 10 < 5) true

Examples of logical Operators

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 21;
    char ch = 'A';
    if (x > 9 && x < 100) // logical and
        printf("This is Two Digit number %d
", x);
    if ('a' == ch || 'A' == ch) // logical or
        printf("This is first letter of English
");
    if (!0) // logical not
        printf("This is true
");
    return 0;
}

Comparison Operators

These comparison operators are used to compare two variables value or its types. we have already use these operators in above Examples.

operators descriptions comparing return value
== equal to 10 == 20 false
!= not equal "20" != 20 false
< less than 20 < 100 true
> greater than 20 > 100, "100">"20", 20>20 false
>= greater than or equal 20 >= 20 , 20 > = 10 true
<= less than or equal 20<=20 , 20 <=100 true
? : ternary or conditional oprator 20>10? yes : no yes

Assignment Operator

Assignment operator is used to assign value in a variable. At the left side of operator must be a variable.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 21;  // assignment operator
    printf("The value of x is %d
",x);
    return 0;
}

? : Ternary Operator(Conditional Operator)

conditional operator is same as if else statement but inside this ternay operator we cannot use some statements.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int x = 2;
    int y = 10;
    printf("greater number is %d
",x>y ? x : y);
    return 0;
}