×
>
<

C++ Programming

C++ Operators | CrackEase

Operators In C

What is an Operator ?

Operators are symbols used to perform mathematical, relational, logical and bitwise operations on data and variables. An expression is a combination of operands and operators. Operands are the values or variables on which operators act.

Operator Types
operator types

Operators are broadly classified as:

  • Unary operators — operate on a single operand.
  • Binary operators — operate on two operands.

Unary operator: used with one operand.

Binary operator: used with two operands.

Arithmetic operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division and modulus. Assume A = 5 and B = 10

OperatorFunctionExample
+Adds two operands (numbers)A + B = 15
-Subtracts second operand from the firstA - B = -5
*Multiplies the operandsA * B = 50
/Divides numerator by denominatorB / A = 2
%Returns remainder after divisionB % A = 0
++Increment operator — increases integer value by one++A = 6 (pre-increment)
--Decrement operator — decreases integer value by oneA-- = 4 (post-decrement)
Logical Operators
logical operators

Logical operators are used in decision-making and conditional statements to combine or invert logical expressions. Assume A = 5, B = 10.

OperatorFunctionExample
&&Logical AND — true if both operands are true(A > 8 && B == 10) is false
||Logical OR — true if any operand is true(A > 8 || B == 10) is true
!Logical NOT — inverts the logical state!(A > 8 || B == 10) is false
Bitwise operators

Bitwise operators operate on the binary representation (bits) of integer values. For example A = 50 (0011 0010) and B = 10 (0000 1010).

OperatorFunctionExample
&Bitwise AND — copies bit to result if it is set in both operands(A & B) = 2
|Bitwise OR — copies bit if set in either operand(A | B) = 58
^Bitwise XOR — copies bit if set in one operand only(A ^ B) = 56
~Bitwise NOT — inverts all bits(~A) = -51 (two's complement)
<<Left shift — shifts bits leftA << 2 = 200
>>Right shift — shifts bits rightA >> 2 = 12
Relational operators

Relational operators compare two values and return a boolean result (true/false). Assume A = 5, B = 10.

OperatorFunctionExample
==Equal to — true if operands are equal(A == B) is false
!=Not equal to — true if operands are not equal(A != B) is true
>Greater than(A > B) is false
<Less than(A < B) is true
>=Greater than or equal to(A >= B) is false
<=Less than or equal to(A <= B) is true
Assignment operators

Assignment operators assign values to variables. Compound assignment operators perform an operation and assignment in one step.

OperatorFunctionExample
=Assign right operand to leftC = A + B
+=Add and assignC += A (C = C + A)
-=Subtract and assignC -= A (C = C - A)
*=Multiply and assignC *= A (C = C * A)
/=Divide and assignC /= A (C = C / A)
%=Modulus and assignC %= A (C = C % A)
<<=Left shift and assignC <<= 2
>>=Right shift and assignC >>= 2
&=Bitwise AND and assignC &= 2
^=Bitwise XOR and assignC ^= 2
|=Bitwise OR and assignC |= 2
Miscellaneous operators

Other useful operators:

OperatorFunctionExample
sizeof()Returns size (in bytes) of a type or variablesizeof(a)
&Address-of operator — returns address of a variable&a
*Dereference operator (pointer access)*p
?:Conditional (ternary) operator — shorthand for if-elsecond ? X : Y
Footer Content | CrackEase