× Home Operators in C Arithmetic Relational Logical Bitwise Assignment Miscellaneous Precedence
Next Lec → ← Previous Lec

Operators in C

Basic Operators and Functions

Before that we should know

                        

                            int a;
                            scanf("%d",&a);
                            // This is a function which enables program to take input from user.
                        
                    

If we want to store number which have decimal in it so we should use float or double.

Installing Code Runner Extension For VS Code

Search Code Runner

This extension will help us run our program directly

So What is an Operator?

Types of Operators in C Language

Arithmetic Operators

+ → Addition
- → Substraction
* → Multiplication
/ → Division
%→ Modulus (gives remainder)

                        

                                #include <stdio.h>

                                int main() {
                                   int a, b;
                                   a = 34;
                                   b = 6;
                                
                                   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);
                                   return 0;
                                } 
                        
                    

output
a + b = 40
a - b = 28
a * b = 204
a / b = 5

Relational Operators

== → Is equal to
!= → Is not equal to
> → Greater than
< → Less than
>= → Greater than or equal to
<= → Less than or equal to

These returns true(1) or false(0) value

used in conditional statements

Logical Operator

&& → Logical AND operator: If both the operands are non-zero, then the condition is true.
|| → Logical OR operator: If any of these two operands is non-zero, then condition becomes true.
! → Logical NOT operator: It is used to reverse the logical state of its operand. If condition is true, then logical NOT operator will make it false.

Bitwise Operators

To perform bit level operations, bitwise operators are used. They convert the values we provide to them in binary format and then compare them to provide us the results.

& → Bitwise AND
| → Bitwise OR
^ → Bitwise XOR
~ → Bitwise complement
<< → Shift left
>> →Shift right

Assignment Operator

☆ = → 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 substracts the right operand from the left operand and the result is addigned to the left operand.
☆ *= → Multiply AND assignment operator. It multiplies the right operand with the left operand and the result is assigned to the left operand.
☆ /= → Divide AND assignment operator. It divides the left operand with the operand and the result is assigned to the left operand.

Miscellaneous Operators

☆ sizeof() → Returns the size of a variable. → sizeof(a), where a is integer, will return int's size on that architecture.
☆ & → Returns the address of a variable. → &a; returns the actual address of the variable.
☆ * → Pointer to a variable → *a;
☆ ? : → Conditional Expression → If condition is true ? then value X : otherwise value Y

Operator Precedence in C

In programming languages, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence or parenthesis.

Category

Operator

Associativity

Postfix

() [] -> . ++ --

Left to right

Unary

+ - ! ~ ++ -- (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Conditional

? :

Right to left

Assignment

= += -= *= /= %= >>= <<= &= ^= !=

Right to left

Comma

,

Left to right