Home > C Programming, C Tutorials > C Tutorial – Operators in C

C Tutorial – Operators in C

May 25th, 2008

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.

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

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
/ 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++;

Program Output

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;

Program Output

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


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


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

a&gt;b && a+b&gt;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&gt;b || a+b&gt;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.


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.







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)


c = (a&gt;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.

  1. Colombia
    December 19th, 2008 at 03:57 | #1

    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. 🙂

  2. January 13th, 2009 at 18:27 | #2

    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.

  3. umesh
    February 25th, 2009 at 14:26 | #3

    please check for the program….

    void main()
    { int a=10,b;



  4. parveen
    June 8th, 2009 at 19:44 | #4

    I need programs for each topic please send programs

  5. admin
    June 10th, 2009 at 21:02 | #5

    Hi Praveen,

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

    Subscribe to my newsletter to be updated.

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

  6. A.MOHAN
    June 18th, 2009 at 12:11 | #6

    really nice

  7. sham
    June 22nd, 2009 at 23:12 | #7

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


  8. Sunny
    August 11th, 2009 at 18:34 | #8

    Hi umesh

    ur ans is correct for output of postfix notation



  9. aies
    October 12th, 2009 at 10:35 | #9

    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 :-
    int a=3,b;
    b=a++ + ++a;

  10. ujas
    October 17th, 2009 at 14:18 | #10


  11. nitya
    November 5th, 2009 at 21:01 | #11

    say about residence of operation

  12. chandan
    November 12th, 2009 at 09:13 | #12

    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…

  13. December 12th, 2009 at 22:23 | #13

    In the postfix example, the output will be


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

  14. December 16th, 2009 at 00:08 | #14

    plz send me a program of the pallindrom

  15. Abhay
    February 4th, 2010 at 13:39 | #15

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

    int a=3,b;
    b=a++ + ++a;

  16. admin
    February 4th, 2010 at 14:15 | #16

    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.


  17. Areeba
    January 2nd, 2015 at 13:12 | #17

    It is quite helpful.You must be appreciated for your effort. Can you please explain this code bcoz I think it should print 23 but it prints 24
    int a=10,b;
    b=++a + ++a;

Comments are closed.