0% found this document useful (0 votes)
11 views

Operators

Uploaded by

princepatna936
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Operators

Uploaded by

princepatna936
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

OPERATORS

An Operator is a symbol used to perform an operation between two operands.

Types of Operators.
Arithmetic Operators.

Relational Operators.

Logical Operators.

Bitwise Operators.

Assignment Operators.

1. Arithmetic Operators:
Used to perform basic mathematical operations.

Examples:

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus, remainder)

2. Relational (Comparison) Operators:


They are used to compare two values and return a boolean result ( true or false ).

Examples:

== (equal to)

!= (not equal to)

OPERATORS 1
> (greater than)

< (less than)

>= (greater than or equal to)

<= (less than or equal to)

3. Logical Operators:
Used to combine boolean expressions.

Examples:

&& (logical AND)

|| (logical OR)

! (logical NOT)

4. Bitwise Operators:
Operate at the bit level on binary numbers.

Examples:

& (bitwise AND)

| (bitwise OR)

^ (bitwise XOR)

<< (left shift)

>> (right shift)

a b a&b a|b a^b

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

5. Assignment Operators:

OPERATORS 2
Used to assign values to variables.

Examples:

= (assignment)

+= (add and assign)

- = (subtract and assign)

* = (multiply and assign)

/= (divide and assign)

What is Unary, Binary and Ternary?

What is Unary?
A unary operator operates on one operand.

It performs operations like incrementing and decrementing value.

Examples:

++ (increment)

-- (decrement)

int x = 5;
x++; // this will increment value of x by one (post
printf("%d",x);

int x = 5;
x--; // this will decrement value of x by one (post
printf("%d",x);

int x = 5;
++x; // this will decrement value of x by one (pre in

OPERATORS 3
printf("%d",x);

int x = 5;
--x; // this will decrement value of x by one (pre de
printf("%d",x);

What is Binary?
A binary operator operates on two operands.

It's used for arithmetic, comparisons, logic, and assignments between two
values.

Examples:

+ (addition)

- (subtraction)

== (equality check)

&& (logical AND)

int a = 10;
int b = 20;
int c = a+b; // this operator will add a and b values
printf("%d",c);

int a = 10;
int b = 20;
int c = b-a; // this operator will subtract value of a from b
printf("%d",c);

int a = 10;
int b = 10;
if(a == b){ // this is check equallity with two operands

OPERATORS 4
printf("Equal");
}

if(5 < 7 && 7 > 5){ // this is check true both sides with &&
printf("True");
}

What is ternary?
A ternary operator operates on three operands.

It is typically used for conditional expressions as a shorthand for if-else

statements.

Syntax: condition? value_if_true : value_if_false

int a = 10;
int b = 20;
int max = (a > b) ? a : b; // Ternary operator, returns 20

Operator Precedence In c
Operator
Precedence Description Associativity

1 () Parentheses (function call) Left-to-Right

[] Array Subscript (Square Brackets)

. Dot Operator

-> Structure Pointer Operator

++ , — Postfix increment, decrement

2 ++ / — Prefix increment, decrement Right-to-Left

+/– Unary plus, minus

!,~ Logical NOT, Bitwise complement

(type) Cast Operator

OPERATORS 5
* Dereference Operator

& Addressof Operator

sizeof Determine size in bytes

3 *,/,% Multiplication, division, modulus Left-to-Right

4 +/- Addition, subtraction Left-to-Right

5 << , >> Bitwise shift left, Bitwise shift right Left-to-Right

Relational less than, less than or


6 < , <= Left-to-Right
equal to

Relational greater than, greater than


> , >=
or equal to

7 == , != Relational is equal to, is not equal to Left-to-Right

8 & Bitwise AND Left-to-Right

9 ^ Bitwise exclusive OR Left-to-Right

10 | Bitwise inclusive OR Left-to-Right

11 && Logical AND Left-to-Right

12 || Logical OR Left-to-Right

13 ?: Ternary conditional Right-to-Left

14 = Assignment Right-to-Left

+= , -= Addition, subtraction assignment

*= , /= Multiplication, division assignment

%= , &= Modulus, bitwise AND assignment

Bitwise exclusive, inclusive OR


^= , |=
assignment

<<=, >>= Bitwise shift left, right assignment

15 , comma (expression separator) Left-to-Right

Practice Questions

1. What are operators in programming, and why are they important?

2. What are arithmetic operators, and why are they used in programming?

OPERATORS 6
3. Write a program to perform addition using arithmetic operators.

4. Write a program to perform subtraction using arithmetic operators.

5. Write a program to perform multiplication using arithmetic operators.

6. Write a program to perform division using arithmetic operators.

7. Write a program to perform post-increment.

8. Write a program to perform pre-increment.

9. Write a program to perform post-decrement.

10. Write a program to perform pre-decrement.

11. What is the syntax of the ternary operator?

12. Write a C program using the ternary operator to find the maximum of two
numbers.

OPERATORS 7

You might also like