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

Operators

Uploaded by

shreyas19052006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Operators

Uploaded by

shreyas19052006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C Dennis Ritchie

Operators
An operator is a symbol that tells the compiler to perform a specific
operation on data or variables. Operators are used to manipulate variables and
values in various ways, like performing arithmetic, comparisons, or logical
operations.
Types of Operators
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Conditional Operator
Special Operator
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on
variables and data.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation
#include <stdio.h>
void main() {
int a = 9,b = 4, c; Output
c = a+b; //Addition a+b = 13
printf("a+b = %d \n",c); a-b = 5
c = a-b;//Subtraction a*b = 36
printf("a-b = %d \n",c); a/b = 2
c = a*b; //Multiplication Remainder when a divided by b=1
printf("a*b = %d \n",c);
c = a/b; //Division
printf("a/b = %d \n",c);
c = a%b; //Modulus
printf("Remainder when a divided by b = %d \n",c);
}
C Dennis Ritchie
Increment and Decrement Operators
++ increases the value of the operand by 1
-- decreases it by 1
These operators can be applied in two ways:
· Prefix (before the variable): ++x or --x
· Postfix (after the variable): x++ or x--
1. Prefix Increment/Decrement (++x, --x)
· Increment (++x): Increases the value of the variable first and then uses
the updated value in the expression.
· Decrement (--x): Decreases the value of the variable first and then uses
the updated value in the expression.
Example:
int x = 5;
int y = ++x; // x is incremented to 6, and then y is assigned the value 6.
printf("x = %d, y = %d\n", x, y); // Output: x = 6, y = 6
2. Postfix Increment/Decrement (x++, x--)
· Increment (x++): The variable is used first in the expression and then
incremented.
· Decrement (x--): The variable is used first in the expression and then
decremented.
Example:
int x = 5;
int y = x++; // y is assigned the value 5, and then x is incremented to 6.
printf("x = %d, y = %d\n", x, y); // Output: x = 6, y = 5

#include <stdio.h>
void main() {
Output
int a = 10, b = 100;
++a = 11
float c = 10.5, d = 100.5;
--b = 99
printf("++a = %d \n", ++a);
++c = 11.500000
printf("--b = %d \n", --b);
--d = 99.500000
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
}
C Dennis Ritchie
Assignment Operators

Operator Example Equivalent to


= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

a=3
a+=10
can be written has a=3+10
a = a + 10
a=13
Assignment Addition

#include <stdio.h>
void main()
{
int a = 5, c=10;
c = a;
printf("c = %d\n", c);
c += a;
printf("c = %d\n", c);
c -= a;
printf("c = %d\n", c);
c *= a;
printf("c = %d\n", c);
c /= a;
printf("c = %d\n", c);
c %= a;
printf("c = %d\n", c);
}

Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C Dennis Ritchie
Relational Operators
A relational operator is used to check the relationship between two operands
If the relation is true, it returns 1 whereas if the relation is false, it returns 0
Operator Meaning
== Is Equal to
!= Not equal to
> Greater than
< Lesser than
>= Greather than or equal to
<= Lesser than or equal to

Operator Examples

5==5 It is evaluated to 1
==
5==2 It is evaluated to 0

!= 5!=5 It is evaluated to 0
5!=2 It is evaluated to 1

> 5>3 It is evaluated to 1


5>8 It is evaluated to 0

< 5<3 It is evaluated to 1


5<8 It is evaluated to 0

>= 5>=3 It is evaluated to 1


5>=8 It is evaluated to 0
5<=3 It is evaluated to 0
<=
2<=3 It is evaluated to 1

#include <stdio.h>
void main()
{
int a = 5, b = 5; 5 == 5 is 1
printf("%d == %d is %d \n", a, b, a == b); 5 > 5 is 0
printf("%d > %d is %d \n", a, b, a > b); 5 < 5 is 0
printf("%d < %d is %d \n", a, b, a < b); 5 != 5 is 0
printf("%d != %d is %d \n", a, b, a != b); 5 >= 5 is 1
printf("%d >= %d is %d \n", a, b, a >= b); 5 <= 5 is 1
printf("%d <= %d is %d \n", a, b, a <= b);
}
C Dennis Ritchie
Logical Operators
Logical operators are used to check whether an expression is true or false.
If the expression is true, it returns 1 whereas if the expression is false,
it returns 0.
Operator Meaning

&& Logical AND

|| Logical OR

! Logical NOT
AND OR NOT
a b a&&b a b a||b a !a

0 0 0 0 0 0 0 1

0 1 0 0 1 1 1 0

1 0 0 1 0 1

1 1 1 1 1 1

#include <stdio.h>
void main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);
Output
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
(a == b) && (c > b) is 1
result = !(a != b); (a == b) && (c < b) is 0
printf("!(a != b) is %d \n", result); (a == b) || (c < b) is 1
(a != b) || (c < b) is 0
result = !(a == b); !(a != b) is 1
printf("!(a == b) is %d \n", result); !(a == b) is 0
}
C Dennis Ritchie
Bitwise Operators
Bitwise operators are used to perform operations at the bit-level on variables.
This means they directly manipulate the bits (0s and 1s) of integers
Operator Meaning

& Binary AND

| Binary OR

^ Binary XOR

~ Binary NOT

<< Binary Shift Left

>> Binary Shift Right


Here's the simple difference between logical and bitwise operators:
1. Logical Operators:
· Work with true or false conditions.
· Used in decisions like if statements.
Example: && (AND) checks if both conditions are true.
int a = 5, b= 10;
if (a == 5 && b == 10) { // Checks if both a and b are true (whole values)
printf("Both are true\n");
}
2. Bitwise Operators:
· Work on the bits (0s and 1s) inside numbers.
· Used to manipulate data at the bit level.
Example: & (AND) compares each bit in two numbers
int a = 5, b = 3;
int result = a & b; // Compares bits: 5 = 0101 and 3 = 0011 -> result = 0001 (1)
printf("Result is %d\n", result);

X Y X&Y X|Y X^Y

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0
C Dennis Ritchie

#include <stdio.h>

void main() {
int a = 5, b = 3;

printf("a & b = %d\n", a & b); // Bitwise AND


printf("a | b = %d\n", a | b); // Bitwise OR
printf("a ^ b = %d\n", a ^ b); // Bitwise XOR
printf("~a = %d\n", ~a); // Bitwise NOT
printf("a << 1 = %d\n", a << 1); // Left Shift
printf("a >> 1 = %d\n", a >> 1); // Right Shift
}

Output:

a&b=1
a|b=7
a^b=6
~a = -6
a << 1 = 10
a >> 1 = 2
C Dennis Ritchie
Conditional Operator
The conditional operator (also known as the ternary operator) in C is a
shorthand way to write an if-else statement. It evaluates a condition and returns
one of two values depending on whether the condition is true or false.
Syntax:

condition ? expression_if_true : expression_if_false;


· condition: This is the condition to check (true or false).
· expression_if_true: This is executed if the condition is true.
· expression_if_false: This is executed if the condition is false.

Example:

int a = 10, b = 20;


int max = (a > b) ? a : b; // If a > b is true, max = a, otherwise max = b
printf("Max is %d\n", max);

How it works:
· Condition: (a > b) checks if a is greater than b.
· If true, it assigns a to max.
· If false, it assigns b to max.
· In this case, since a is 10 and b is 20, max will be 20.

Program for even or odd using conditional operator


#include <stdio.h>

void main() {
int number = 5,result;
result = (number % 2 == 0) ? "Even" : "Odd";
printf("The number %d is %s.\n", number, result);
}
C Dennis Ritchie
Special Operator
1. Sizeof Operator

· Purpose: Determines the size (in bytes) of a data type or variable.

· Syntax: sizeof(type) or sizeof variable

2. Comma Operator

· Purpose: Allows you to evaluate multiple expressions in a single statement,


returning the value of the last expression.

· Syntax: expression1, expression2

· Example:
int x = (1, 2, 3); // x will be assigned the value 3
printf("%d\n", x); // Output: 3

#include <stdio.h>
void main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));

return 0;
}

Output

Size of int = 4 bytes


Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
C Dennis Ritchie
Type casting in C is the process of converting a variable from one data type to
another. It allows you to change the type of a variable temporarily for a specific
operation or assignment.

Types of Type Casting

1. Implicit Type Casting (Automatic)

o This occurs automatically when you assign a value of a smaller


data type to a larger data type. The compiler handles the
conversion.

o Example:
int a = 5;
float b = a; // Implicitly converts int to float
printf("%f\n", b); // Output: 5.000000

2. Explicit Type Casting (Manual)

o This is done manually by the programmer using the casting


operator. It is used when you want to convert a larger data type to a
smaller one or to a different type.

o Syntax: (type) expression

o Example:
float x = 5.7;
int y = (int)x; // Explicitly converts float to int
printf("%d\n", y); // Output: 5
#include <stdio.h>
int main() {
int a = 10;//implicit type casting
float b = a; // int to float (automatic)
printf("Implicit casting: %f\n", b); // Output: 10.000000
float c = 9.99; // explicit type casting
int d = (int)c; // float to int (manual) Output:
printf("Explicit casting: %d\n", d); // Output: 9 Implicit casting: 10.000000
Explicit casting: 9
return 0;
}
C Dennis Ritchie
Associativity
It refers to the order in which operators of the same precedence are
evaluated in an expression. It determines how expressions are grouped when
there are multiple operators of the same precedence.
Types of Associativity
1. Left-to-Right Associativity:
o Operators are evaluated from left to right.
o Most arithmetic operators, comparison operators, and logical
operators have left-to-right associativity.
o Example:
int a = 10, b = 5, c = 2;
int result = a - b + c; // Evaluated as (a - b) + c
// Step 1: a - b = 5
// Step 2: 5 + c = 7
printf("%d\n", result); // Output: 7
2. Right-to-Left Associativity:
o Operators are evaluated from right to left.
o The assignment operator (=) and the conditional operator (?:) have
right-to-left associativity.
o Example:
int x, y, z;
x = y = z = 5; // Evaluated as x = (y = (z = 5))
// Step 1: z = 5 (z is now 5)
// Step 2: y = 5 (y is now 5)
// Step 3: x = 5 (x is now 5)
printf("%d %d %d\n", x, y, z); // Output: 5 5 5

+, -, *, /, % Left-to-right
==, !=, <, >, <=, >= Left-to-right
= (assignment) Right-to-left
?: (conditional) Right-to-left

You might also like