## C Tutorial – Operators in C

An operator is a symbol that operates on a certain data type and produces the output as the result of the operation. In C, you can combine various operators of similar or different categories and perform an operation. In this case the C compiler tries to solve the expression as per the rules of precedence.

### Category of operators

**Unary Operators**

A unary operator is an operator, which operates on one operand.

**Binary**

A binary operator is an operator, which operates on two operands

**Ternary**

A ternary operator is an operator, which operates on three operands.

### Following are the different types of Operators

Arithmetic Operator

The arithmetic operator is a binary operator, which requires two operands to perform its operation of arithmetic. Following are the arithmetic operators that are available

Operator | Description |

+ | Addition |

- | Subtraction |

/ | Division |

* | Multiplication |

% | Modulo or remainder |

At the time of using the â€˜/â€™ division operator on two integers, the result will also be an integer. E.g. 100/8 will result in 12 and not 12.50.

### Unary Operators

A unary operator is an operator, which operates on one operand, i.e. it operates on itself.

Following are the unary operators available under C

! & * - + ~

**Increment and Decrement Operators**

These operators also fall under the broad category or unary operators but are quite distinct than unary minus.

The increment and decrement operators are very useful in C language. They are extensively used in for and while loops. The syntax of these operators is given below.

++

–

The ++ operator increments the value of the variable by one, whereas the â€“ operator decrements the value of the variable by one.

These operators can be used in either the postfix or prefix notation as follows:

Postfix: a++ or a–

Prefix: ++a or â€“a

**Postfix notation**

In the postfix notation, the value of the operand is used first and then the operation of increment or decrement takes place, e.g. consider the statements below:

int a,b; a = 10; b = a++; printf(â€œ%d\nâ€,a); printf(â€œ%d\nâ€,b);

**Program Output**

10

11

**Prefix notation**

In the prefix notation, the operation of increment or decrement takes place first after which the new value of the variable is used.

int a,b; a = 10; b = ++a; printf(â€œ%d\nâ€,a); printf(â€œ%d\nâ€,b);

**Program Output**

11

11

### Relational Operators

Relational operators compare between two operands and return in terms of true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. Relational operators are used in conjunction with logical operators and conditional & looping statements.

Following are the various relational operators

< | Less than |

<= | Less than or equal to |

> | Greater than |

>= | Greater than or equal to |

Closely related to the relational operators is the equality operator as follows:

== | Equal to |

!= | Not equal to |

**Example:**

Suppose we have three integer variables a, b and c with values 1,2 and 3 respectively.

Expression |
Returns |
Meaning |

A<b | 1 | True |

(a+b)>=c | 1 | True |

(b+c)>(a+5) | 0 | False |

(c!=3) | 0 | False |

B==2 | 1 | True |

### Logical Operators

A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available in the C language.

Operator |
Meaning |

&& | Logical AND |

|| | Logical OR |

! | Logical NOT |

Truth table for Logical AND

1 | 1 | 1 |

1 | 0 | 0 |

0 | 1 | 0 |

0 | 0 | 0 |

Truth table for Logical OR

1 | 1 | 1 |

1 | 0 | 1 |

0 | 1 | 1 |

0 | 0 | 0 |

**Example**

Let us take three variables a, b, and c and assume a = 5,b=10 and c=7

a>b && a+b>c

Here a>b will return false hence 0 and a+b>c will return true hence 1. Therefore, looking at the truth table 0 AND 1 will be 0. From this we can understand, that this expression will evaluate to false.

**Let us look at the same example using Logical OR (||)**

a>b || a+b>c

Here a>b will return false hence 0 and a+b>c will return true hence 1. Therefore, looking at the truth table 0 OR 1 will be 1. From this we can understand, that this expression will evaluate to true.

### The Logical NOT operator (!)

The ! (Logical NOT) operator is a unary operator. This operator inverses a logical result.

E.g. if a = 5 and b = 7, then

!(a == b)

will evaluate to be true. This expression works as follows, a==b evaluates to be false, now because of the ! (NOT) operator, the false is inverted to be true.

### Assignment Operator

An assignment operator (=) is used to assign a constant or a value of one variable to another.

**Example:**

a = 5; b = a; interestrate = 10.5 result = (a/b) * 100;

**Important Note:**

Remember that there is a remarkable difference between the equality operator (==) and the assignment operator (=). The equality operator is used to compare the two operands for equality (same value), whereas the assignment operator is used for the purposes of assignment.

**Multiple assignments:**

You can use the assignment for multiple assignments as follows:

a = b= c = 10;

At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.

**Arithmetic Assignment Operators**

Arithmetic Assignment operators are a combination of arithmetic and the assignment operator. With this operator, the arithmetic operation happens first and then the result of the operation is assigned.

**Operators**

+=

-=

*=

/=

%=

Example |
Expands as |

Sum+=3 | Sum = sum + 3 |

Count-=4 | Count -= 4 |

Factorial*=num | Factorial = factorial * num |

Num/=10 | Num = num /10 |

A%=3 | A = a % 3 |

**Conditional Operator**

A conditional operator checks for an expression, which returns either a true or a false value. If the condition evaluated is true, it returns the value of the true section of the operator, otherwise it returns the value of the false section of the operator.

Its general structure is as follows:

Expression1 ? expression 2 (True Section): expression3 (False Section)

**Example:**

a=3,b=5,c; c = (a>b) ? a+b : b-a;

The variable c will have the value 2, because when the expression (a>b) is checked, it is evaluated as false. Now because the evaluation is false, the expression b-a is executed and the result is returned to c using the assignment operator.

### Precedence of Operators

Operator |
Associavity |

Unary | Right to Left |

Arithmetic Operators | Left to Right |

Relational Operator | Left to Right |

Equality Operator | Left to Right |

Logical Operator | Left to Right |

Conditional Operator | Right to Left |

Assignment Operator | Right to Left |

Comma operator | Right to Left |

### Type Conversion

When an operatorâ€™s operands are of different data types, C automatically converts them to the same data type. This can affect the results of mathematical expressions. This is called type conversion. In C type conversion can be done by two ways, viz.

**Automatic Conversion (Arithmetic Promotion)**

When a value is converted to a higher type, it is said to be promoted. Letâ€™s look at the specific rules that govern the evaluation of mathematical expressions.

**Rule 1:** chars, shorts, unsigned shorts are automatically promoted to int.

**Rule 2:** When an operator works with two values of different data types, the lower data type is promoted to a higher data type.

**Type Casting**

Type casting means to convert a higher data type to a lower data type. For the purpose of type casting we require to use the type case operator, which lets you manually promote or demote a value. It is a unary operator which appears as the data type name followed by the operand inside a set of parentheses. E.g.

val = (int)number;

This was about Operators in C, in the next **C Tutorial** you will learn about input and output in C.

Subscribe now to receive updates when a new **C tutorial** is released.

I really liked your blog. Very useful information, I read it detail, also book-marked it and will be back in the future to read some more of your interesting posts ! keep up the good work.

Good resource for the beginners. You are feeding things step by step. The big way you are using to teach people. The explanation for both post and pre incremental operator were good. Pre increment operator explanation is very essential, because more people confused by that one.

please check for the program….

void main()

{ int a=10,b;

b=a++;

printf(“%d\n”,a);

printf(“%d\n”,b);

}

output:

11

11

{

not

10

11

}

I need programs for each topic please send programs

Hi Praveen,

I am working on creating all the programs – which will be available as a download.

Subscribe to my newsletter to be updated.

Regards,

Suniil

http://www.twitter.com/sunilbhatia79 – Follow Me

really nice

the output for program which was present in the post fix notation will be

11

10

not

10

11

Hi umesh

ur ans is correct for output of postfix notation

11

10

not

10

11

Regard

Sunny

increment and decrement operators should be in more details,like their precedence & associativity, how these can b evaluated n in which order…what is the o/p of following prog,how it is evalauted step by step :-

main()

{

int a=3,b;

b=a++ + ++a;

printf(“a=%d\nb=%d”,a,b);

}

superb.unexpected

say about residence of operation

thanks for making such nice,useful and very knowledgeable blog

i m sure any beginner can learn c from here.

thanks again

i want to know about types of error in c

like syntax error,runtime error,compiler error,etc…

In the postfix example, the output will be

11

10

But a good blog to give introduction to programming in C.

plz send me a program of the pallindrom

yeah could you be more elucidative to explain the output of the program

main()

{

int a=3,b;

b=a++ + ++a;

printf(â€a=%d\nb=%dâ€,a,b);

}

Hi Abhay,

As per your above program… following is the explanation of the line

b = a++ + ++a;

For the the operation is from left to right, therefore first a++ is executed. Since a++ uses the postfix notation, current value of a (i.e. 3) is used for the addition (+) operation. Therefore the operation now looks like this:

Step 1: 3 + ++a;

After this, value of a is incremented (due to a++ postfix notation). Therefore now the value of a becomes 4.

Step 2: 3 + ++a(4);

Now, before evaluating the the addition operation (+), ++a is executed because its prefix. Therefore the current value of 4 is incremented to 5;

Step 3: 3 + 5;

Once this is done, 3 + 5 results in 8 and is assigned to b.

Hope this helps.

Regards,

Sunil