×
>
<

Aptitude

C Operators | CrackEase

Operators In C

C operators

What is an operator?

An operator is a symbol that tells the compiler to perform a specific mathematical, logical, bitwise or other operation on one or more operands. An operand is a value, variable or expression on which the operator acts. A combination of operands and operators forms an expression.

Operators are the building blocks for computations and decision-making in C programs.

Types ?
operator types

Operator categories

Operators in C are usually grouped by how many operands they work on and by their functionality:

  • By arity: Unary (one operand), Binary (two operands), and Ternary (?: conditional operator — three operands).
  • By purpose: Arithmetic, Relational, Logical, Bitwise, Assignment, and Miscellaneous (sizeof, address-of, pointer dereference, conditional).
Arithmetic operators
Arithmetic operators

Arithmetic operators perform common mathematical operations. Assume A = 5 and B = 10.

OperatorMeaningExample (A=5, B=10)
+AdditionA + B = 15
SubtractionA − B = -5
*MultiplicationA * B = 50
/Division (integer or floating)B / A = 2
%Modulus — remainder of integer divisionB % A = 0
++Increment by 1 (prefix/postfix)++A → 6 (prefix), A++ uses old value then increments
--Decrement by 1 (prefix/postfix)--A → 4 (prefix), A-- uses old value then decrements
Logical Operators
logical operators

Logical operators are used to combine or invert boolean expressions (usually in control flow). Assume A = 5, B = 10.

OperatorMeaningExample
&&Logical AND — true if both operands are true(A > 0 && B == 10) → true
||Logical OR — true if any operand is true(A > 8 || B == 10) → true
!Logical NOT — negates the boolean value!(A > 8) → true (because A > 8 is false)
Bitwise operators

Bitwise operators operate on the binary representation of integer types. They are useful for low-level programming and masks.

Example: A = 50 (binary 00110010), B = 10 (binary 00001010).

OperatorMeaningResult (A=50, B=10)
&Bitwise AND — bit is 1 only if both bits are 1(A & B) = 2
|Bitwise OR — bit is 1 if any bit is 1(A | B) = 58
^Bitwise XOR — bit is 1 if bits differ(A ^ B) = 56
~Bitwise NOT — flips all bits (two's complement)(~A) = -51 (on typical two's-complement systems)
<<Left shift — shifts bits left (multiplies by 2 per shift)A << 2 = 200
>>Right shift — shifts bits right (divides by 2 per shift, implementation-defined for signed)A >> 2 = 12
Relational operators

Relational operators compare two values and yield boolean results (true or false). They are commonly used in conditional statements.

OperatorMeaningExample (A=5, B=10)
==Equality check(A == B) → false
!=Not equal(A != B) → true
>Greater than(A > B) → false
<Less than(A < B) → true
>=Greater than or equal(A >= B) → false
<=Less than or equal(A <= B) → true
Assignment operators

Assignment operators assign values to variables. Compound assignment operators combine an operation with assignment.

OperatorMeaningEquivalent
=Simple assignmentC = 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 → C = C << 2
>>=Right-shift and assignC >>= 2 → C = C >> 2
&=Bitwise AND and assignC &= 2 → C = C & 2
^=Bitwise XOR and assignC ^= 2 → C = C ^ 2
|=Bitwise OR and assignC |= 2 → C = C | 2
Misc operators

Other useful operators

These operators don't fit into the previous categories but are widely used:

OperatorMeaningExample
sizeof()Returns size in bytes of a type or variablesizeof(int) → typically 4
&Address-of operator — returns the memory address of a variable&a → address of a
*Pointer dereference — accesses value at an address*p → value pointed by p
?:Ternary conditional operatorcond ? expr1 : expr2 → returns expr1 if cond true, else expr2
Footer Content | CrackEase