# C Operators

In this tutorial you will learn about the C Operators and its application with practical example.

An operator is special symbol that is used to perform certain specific operation on its operand.C Programming Language have rich set of built in operators to perform various type of operations. There are operators for assignment, arithmetic functions, logical functions and many more. These operators can be used with many types of variables or constants, but some are restricted to work on specific types. Most operators are binary, meaning they take two operands. A few are unary and only take one operand.

## Type of operators in C

Operators in C programming language can be broadly classified as below –

• Assignment Operators
• Arithmetic Operators
• Increment and Decrement Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators
• Special Operators

### C Assignment Operators

Assignment operators are used to assign value to variable, we can assign a variable value or the result of an arithmetical expression.

Example:-

Here is full list Assignment operators available in C Programming Language –

Operator Meaning Example Description
= Simple assignment operator a = b It assing value of b to a
+= Add and assign a += b It is equivalent to a = a + b
-= Subtract and assign a -= b It is equivalent to a = a- b
*= Multiply and assign a *= b It is equivalent to a = a * b
/= Divide and assign a /= b It is equivalent to a = a / b
%= Modulus and assign a %= b It is equivalent to a = a %b
<<= Right shift and assign a <<= b Right-shifted b bits (assigned to a)
>>= left shift and assign a >>= b Left-shifted b bits (assigned to a)
&= Bitwise AND and assignment a &= b a AND b (assigned to a)
^= Bitwise exclusive OR (XOR) and assignment a ^= b a XOR b (assigned to a)
|= Bitwise inclusive OR and assign a |= b a OR b (assigned to a)

### C Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operations. Here is full list Arithmetic operators available in C Programming Language –

Operator Meaning Example Description
+ Addition operator a + b a plus b
Minus operator a – b a minus b
* Multiply operator a * b a multiply by b
/ Division operator a / b a divided b
% Modulus operator a % b It returns remainder of a/b

### Increment and Decrement Operators (post and pre)

In C Programming Language, ++ and — are know as increment and decrement operators respectively. These are unary operators it means they worls on single operand. ++ adds 1 to operand and — subtracts 1 to operand respectively.When ++ is used as prefix(like: ++i), ++i will increment the value of i and then return it but, if ++ is used as postfix(like: i++), operator will return the value of operand first and then only increment it.

Operator Example Description
++ [prefix] ++a The value of a after increment
++ [postfix] a++ The value of a before increment
— [prefix] –a The value of a after decrement
— [postfix] a– The value of a before decrement

### C Relational Operators

Relational Operators are used evaluate a comparison between two operands. The result of a relational operation is a Boolean value that can only be true or false. Here is full list of Relational operators available in C Programming Language –

Operator Meaning Example Description
== Equal to a == b 1 if a equal to b; 0 otherwise
!= Not equal to a != b 1 if a not equal to b; 0 otherwise
> greater than a > b 1 if a > b; 0 otherwise
< less than a < b 1 if a < b; 0 otherwise
>= greater than or equal to a >= b 1 if a >= b; 0 otherwise
<= less than or equal to a <= b 1 if a <= b; 0 otherwise

### C Logical Operators

Logical operators are used to combine expressions with relational operation using logical (AND,OR,NOT) operators. There are 3 logical operators available in C Programming Language –

Operator Meaning Example Description
&& Logical AND a && b Logical AND of a and b
|| Logical OR a || b Logical OR of a and b
! NOT !a Logical NOT of a

### C Conditional operator ( ? : )

Conditional operator is also know as ‘ternary operator.’

Syntax:-

If condition is true the expression will return result1, if it is not it will return result2.

### C Bitwise Operators

Bitwise operator are used to perform bit level operation over its operand.

Let A = 60; and B = 13;

Binary equivalent

A = 0011 1100

B = 0000 1101

Operator Meaning Example Description
& Binary AND (A & B) It returns 12 which is 0000 1100
| Binary OR (A | B) It returns 12 which is 0000 1100
^ Binary XOR (A ^ B) It returns 49 which is 0011 0001
~ Ones Complement (~A ) It returns -60 which is 1100 0011
<< shift left A << 2 It returns 240 which is 1111 0000
>> shift right A >> 2 It returns 15 which is 0000 1111

### sizeof() Operator In C Programming

It returns the size of its operand’s data type in bytes.

Example:

This will assign the value 1 to a because char is a one-byte long data type.

### Comma operator ( , ) In C

The comma operator (,) allows us to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.

For example, the following code:

• first assign the value 3 to j
• assign j+2 to variable i.

At the end, variable i would contain the value 5 while variable j would contain value 3.

### AddressOf ( & ) Operator In C

Returns the address of an variable.

Example:

### Pointer ( * ) Operator In C Programming

Pointer to a variable.

Example:

### Operators Precedence and Associativity In C Programming

Category Operator Associativity
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

In this tutorial we have learn about the C Operators and its application with practical example. I hope you will like this tutorial.