C Basics

C OPERATORS

User Rating:  / 2
PoorBest 

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C language is rich in built-in operators and provides following type of operators:

  •  Arithmetic Operators
  •  Relational Operators
  •  Logical Operators
  •  Bitwise Operators
  •  Assignment Operators
  •  Misc Operators

 

We will cover each one of above in detail.

 

1) Arithmetic Operators

Operator

Description

+

Adds two operands

-

Subtracts second operand from the first

*

Multiply both operands

/

Divide numerator by de-numerator

%

Modulus Operator and remainder of after an integer division

++

Increment operator increases integer value by one

--

Decrement operator decreases integer value by one

 

2) Relational Operators

 

Operator

Description

==

Checks if the value of two operands is equal or not, if yes then condition becomes true.

!=

Checks if the value of two operands is equal or not, if values are not equal then condition becomes true.

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

>=

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

<=

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

 

3) Logical Operators

 

Operator

Description

&&

Called Logical AND operator. If both the operands are non zero then condition becomes true.

||

Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.

!

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

 

4) Bitwise Operators

Bitwise operator works on bits and perform bit by bit operation. The truth tables for &, |, and ^ are as follows:

 

a

b

a & b

a | b

a ^ b

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

 

Operator

Description

&

Binary AND Operator copies a bit to the result if it exists in both operands.

|

Binary OR Operator copies a bit if it exists in either operand.

^

Binary XOR Operator copies the bit if it is set in one operand but not both.

~

Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.

<< 

Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.

>> 

Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

 

5) Assignment Operators


There are following assignment operators supported by C language: Misc Operators &map; sizeof & ternary. There are few other important operators including sizeof and ? : supported by C Language.

 

Operator

Description

=

Simple assignment operator, Assigns values from right side operands to left side operand

+=

Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand

-=

Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand

*=

Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand

/=

Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand

%=

Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

<<=

Left shift AND assignment operator

>>=

Right shift AND assignment operator

&=

Bitwise AND assignment operator

^=

bitwise exclusive OR and assignment operator

|=

bitwise inclusive OR and assignment operator

 

6) Misc Operators &map; sizeof & ternary
There are few other important operators including sizeof and ? : supported by C Language.

 

Operator

Description

Example

sizeof()

Returns the size of an variable.

sizeof(a), where a is integer, will return 4.

&

Returns the address of an variable.

&a; will give actual address of the variable.

*

Pointer to a variable.

*a; will pointer to a variable.

? :

Conditional Expression

If Condition is true ? Then value X : Otherwise value Y

                                                                                          About Us  ||  Contact Us  ||  Terms & Conditions  ||  Privacy Policy 

Referral Banners