Tech Study

C Operators : Operator in C Programming Language

Operators are fundamental tools that are used to perform mathematical and logical operations. 

Two terms we often come across while performing mathematical operations are:

Operand : An operand can be a variable or value or a result of function.

Operators : An operator can be defined as the operation that is to be performed on/between operands. Each operator’s functionality defers from each other.

Types of C Operators

Type List of operators
Arithmetic Operators + , – , * , / , %
Relational Operators == , != , <= , >= , < , >
Logical Operators ! , && , ||
Assignment Operators = , += , -= , *= , /= , %=
Conditional/Ternary Operator ?:
Bitwise Operators & , | , << , >> , ^ , ~
Increment/Decrement Operators — , ++
Special Operators *var_name(pointer) , &var_name(reference) , sizeof

1. Arithmetic Operators in C

Operator Operand Operation Functionality
+ a,b Addition a+b ; adds two operands
a,b Subtraction a-b ; subtracts one operand from another
* a,b Multiplication a*b ; multiplies two operands
/ a,b Division a/b ; generates the quotient when two operands are divided 
% a,b Modulus a%b ; generates the remainder when two operands are divided

2. Relational Operators in C

Operator Operation Functionality
== Equal to Checks whether two operands are equal
!= Not equal to Checks whether two operands are not equal
<= Lesser than equal to Checks if one operand is lesser than or equals to another
>= Greater than equal to Checks if one operand is greater than or equals to another
< Lesser than Checks if one operand is lesser than another
> Greater than Checks if one operand is greater than another

3. Logical Operators in C

There are two types of Logical operators:

  • Unary operators: works with one operand
  • Binary operators: works with two operands
Operator Operand Operation Functionality
! a Unary not !a ; Returns negation on a constant or variable
&& a,b Binary and a&&b ; checks whether both the operands are true or not
|| a,b Binary or a||b ; checks whether either of the operands are true or not

4. Assignment Operators in C

Operator Operand Operation Functionality
= a,b Assignment a=b ; assigns value from right operand to left operand
+= a,b Addition assignment a+=b ; adds both operands and stores result in left operand
-= a,b Subtraction assignment a-=b ; subtracts one from another operand and stores result in left operand
*= a,b Multiplication assignment a*=b ; multiplies both operands and stores result in left operand
/= a,b Division assignment a/=b ; stores quotient in left operand upon division of both operands
%= a,b Modulus assignment a%=b ; stores remainder in left operand upon division of both operands

5. Conditional/Ternary Operator in C

Conditional operator is used in place of an if-else statement.

Syntax Var_name = (condition) ? True_val : False_val
Example a = ( b > c ) ? 1 : 0 
Functionality If given condition is true returns the true value else returns false value

6. Bitwise Operator in C

Operator Operand Operation Functionality
& a,b Bitwise and a&b ; Returns decimal number upon performing AND operation on each bit of operand against each other (if both bits are 1 returns 1 else 0)
| a,b Bitwise or a|b ; Returns decimal number upon performing OR operation on each bit of operand against each other (if either of the bit is 1 return 1 else 0)
<< a,b Bitwise left shift a<<b ; Shift 1st operand’s bits to specified number of positions(b) to left . 

( result = a * 2 power b)

>> a,b Bitwise right shift a<<b ; Shift 1st operand’s bits to specified number of positions(b) to right . 

( result = a / 2 power b)

^ a,b Bitwise XOR a^b ; Returns decimal number upon performing XOR operation on each bit of operand against each other (if both bits are same returns 0 else 1)
~ a Bitwise one’s complement ~a ; returns one’s complement of operand

7. Increment/Decrement Operators in C

Operator Operation Syntax Functionality
++
  1. Pre- increment
  1. Post-increment
b = ++a

b = a++

Increment operand by 1 before assigning it to b.

Increment operand by 1 after assigning it to b.

  1. Pre- decrement
  1. Post-decrement
b = –a

b = a–

decrement operand by 1 before assigning it to b.

decrement operand by 1 after assigning it to b.

8. Special Operators in C

  1. *var_name (pointer): it stores the address of a variable.
  2. &var_name (reference): it points to the address of existing variable providing a copy/alias of original data.
  3. Sizeof: returns size of the variable.

 

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more