0% found this document useful (0 votes)
73 views7 pages

C Operators Explained: Types & Examples

This document summarizes various C operators including: - Arithmetic operators like addition, subtraction, multiplication, division, modulus, increment, and decrement - Relational operators like equal, not equal, greater than, less than, greater than or equal to, less than or equal to - Logical operators like AND, OR, and NOT - Bitwise operators like AND, OR, XOR, complement, left shift, and right shift - Assignment operators like simple assignment, addition assignment, subtraction assignment etc. - sizeof operator to return the size of a variable - Conditional ternary operator - Pointer related operators like address of, pointer to, dereference

Uploaded by

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

C Operators Explained: Types & Examples

This document summarizes various C operators including: - Arithmetic operators like addition, subtraction, multiplication, division, modulus, increment, and decrement - Relational operators like equal, not equal, greater than, less than, greater than or equal to, less than or equal to - Logical operators like AND, OR, and NOT - Bitwise operators like AND, OR, XOR, complement, left shift, and right shift - Assignment operators like simple assignment, addition assignment, subtraction assignment etc. - sizeof operator to return the size of a variable - Conditional ternary operator - Pointer related operators like address of, pointer to, dereference

Uploaded by

Ashritha kotte
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

C Operators

Pratik Chattopadhyay
Arithmetic Operators
Assume variable A holds 10 and variable B holds 20

Operator Description Example

+ Adds two operands. A + B = 30


− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200


/ Divides numerator by de-numerator. B /A= 2

% Modulus Operator and remainder of after an integer division. B%A=0

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. B-- = 19


Relational Operators
Assume variable A holds 10 and variable B holds 20

Operator Description Example


== Checks if the values of two operands are equal or not. If (A == B) is not true.
yes, then the condition becomes true.
!= Checks if the values of two operands are equal or not. If (A != B) is true.
the values are not equal, then the condition becomes true.
> Checks if the value of left operand is greater than the value (A > B) is not true.
of right operand. If yes, then the condition becomes true.
< Checks if the value of left operand is less than the value of (A < B) is true.
right operand. If yes, then the condition becomes true.
>= Checks if the value of left operand is greater than or equal to (A >= B) is not true.
the value of right operand. If yes, then the condition
becomes true.
<= Checks if the value of left operand is less than or equal to the (A <= B) is true.
value of right operand. If yes, then the condition becomes
true.
Logical Operators
Assume variable A holds 1 and variable B holds 0 (A and B can be expressions with relational operators also)

Operator Description Example


&& Called Logical AND operator. If both the operands are (A && B) is false.
non-zero, then the condition becomes true.

|| Called Logical OR Operator. If any of the two operands (A || B) is true.


is non-zero, then the condition becomes true.

! Called Logical NOT Operator. It is used to reverse the !(A && B) is true.
logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.

Be careful about the double symbols in case of AND and OR


&& and || allow multiple expressions to be combined
Bit-wise Operators
Assume variable A holds 60 and variable B holds 13
Operator Description Example
Binary AND Operator copies a bit to the result if it exists in both
& (A & B) = 12, i.e., 0000 1100
operands.

| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101

^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011 0001

Binary Ones Complement Operator is unary and has the effect of 'flipping' (~A ) = -61, i.e., 1100 0011 in 2's
~
bits. complement form.

Binary Left Shift Operator. The left operands value is moved left by the
<< A << 2 = 240 i.e., 1111 0000
number of bits specified by the right operand.

Binary Right Shift Operator. The left operands value is moved right by the
>> A >> 2 = 15 i.e., 0000 1111
number of bits specified by the right operand.
Assignment Operators
Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side C = A + B will assign the value of A + B
operand to C
+= Add AND assignment operator. It adds the right operand to the left operand and
C += A is equivalent to C = C + A
assign the result to the left operand.
-= Subtract AND assignment operator. It subtracts the right operand from the left
C -= A is equivalent to C = C - A
operand and assigns the result to the left operand.
*= Multiply AND assignment operator. It multiplies the right operand with the left
C *= A is equivalent to C = C * A
operand and assigns the result to the left operand.
/= Divide AND assignment operator. It divides the left operand with the right
C /= A is equivalent to C = C / A
operand and assigns the result to the left operand.
%= Modulus AND assignment operator. It takes modulus using two operands and
C %= A is equivalent to C = C % A
assigns the result to the left operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
sizeof() and Conditional Ternary Operators
Operator Description Example
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
?: If Condition is true ? then value X :
Conditional Expression. otherwise value Y

You might also like