This slide contains information about c++ operators,This slide contains information about This slide contains information about c++ operators,This slide contains information about c++ operators,This slide contains information about c++ operators,This slide contains information about c++ operators,
VIP Call Girl Cuttack Aashi 8250192130 Independent Escort Service Cuttack
This slide contains information about Operators in C.pptx
1. Operators
PRESENTED BY : MR. MADHUR THAPLIYAL
ASSISTANT PROFESSOR
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER APPLICATIONS
2. Definition
Operators in c are used to perform operations on variables and
values.
In example below we used + operator to add together two values:
int myNum = 100+50;
Although the + operator is also used to add together two values, like in
the example above but they can also be used to add together a variable
and a value, or a variable and another variable:
Example;
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
3. Unary Operators
These are operators that act upon a single operand to
produce a new value.
1. unary minus(-)
2. increment(++)
3. decrement(- -)
4. NOT(!)
5. Addressof operator(&)
6. sizeof()
4. Unary Minus
The minus operator changes the sign of its argument. A positive number
negative, and a negative number becomes positive.
int a = 10;
int b = -a; // b = -10
unary minus is different from the subtraction operator, as subtraction req
operands.
5. Increment
increment: It is used to increment the value of the variable by 1.
The increment can be done in two ways:
2.1 prefix increment: In this method, the operator precedes the operand (e.g., ++a). The value of
the operand will be altered before it is used.
int a = 1;
int b = ++a; // b = 2
2.2 postfix increment: In this method, the operator follows the operand (e.g., a++). The value
operand will be altered after it is used.
int a = 1;
int b = a++; // b = 1
int c = a; // c = 2
6. D ECREMENT
3. decrement: It is used to decrement the value of the variable by 1. The decrement can be done in
two ways:
3.1 prefix decrement: In this method, the operator precedes the operand (e.g., – -a). The value of
the operand
will be altered before it is used.
int a = 1; int b = --a; // b = 0
3.2 postfix decrement: In this method, the operator follows the operand (e.g., a- -). The value of
the operand will be altered after it is used.
int a = 1;
int b = a--; // b = 1
int c = a; // c = 0
7. Not(!)
It is used to reverse the logical state
of its operand. If a condition is true,
then the Logical NOT operator will
make it false.
If X is true, then 1X is false
If X is false, then 1X is true
8. Addressof Operator
5. Addressof operator(&): It gives an address of a
variable. It is used to return the memory address
of a variable. These addresses returned by the
address-of operator are known as pointers
because they “point” to the variable in memory.
int a;
int *ptr;
ptr = &a; //address of a is copied to location ptr
9. Sizeof Operator
sizeof(): This operator returns
the size of its operand, in
bytes. The sizeof() operator
always precedes its operand.
11. Arithmetic Operators
Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x
12. Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign the value 10
to a variable called x:
Example: int x = 10;
The addition assignment operator adds a value to a variable
Example : int x = 10;
x += 5;
13. List of Assignment Operators
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
14. Comparison Operator
Comparison operators are used to compare two values.
Note: The return value of a comparison is either true (1) or false (0).
In the following example, we use the greater than operator (>) to
find out if 5 is greater than 3:
int x = 5;
int y = 3;
printf("%d", x > y); // returns 1 (true) because 5 is greater than 3
15. List of all Comparison operators
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
16. Logical Operators
Logical operators are used to determine the logic between variables or
values:
Operator Name Description Example
&& Logical and Returns true if both statements are true x < 5 && x < 10
|| Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result
is true
!(x < 5 && x < 10)
17. Sizeof operator
The memory size (in bytes) of a data type or a variable can be
found with the sizeof operator:
18. Sizeof Operator
The memory size (in bytes) of a data type or a variable can be found with
the sizeof operator:
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%lun", sizeof(myInt));
printf("%lun", sizeof(myFloat)); Output: 4 4 8 1
printf("%lun", sizeof(myDouble));
printf("%lun", sizeof(myChar));