Course Content
History of C Language
0/2
Basic Structure of C Program
0/1
Types of Errors
0/1
Language Fundamentals
0/1
Data Types and Modifiers
0/1
Programming with C Language
About Lesson

Increment & Decrement Operators

Increment and Decrement Operators are the unary operators of C language.  These can be used as a shorthand notation to add 1 or subtract 1 from the existing value of a variable.

The ++ is known as the Increment operator and — is known as the Decrement operator.  The difference between increment/Decrement operators and shorthand notation of assignment operator is, we can use shorthand notation for any operator and for any value but we can use increment/decrement operators for addition and subtraction that too for addition or subtraction of 1 only.

For example,

a=a+1; can be written as a+=1 or a++

a=a+2; can be written as a+=2 but cannot use increment operator.

a=a*8 can be written as a*=8 but there is no other choice

The operand of Increment/Decrement operator must be variable but not a constant/expression.

What is Post/Pre Increment/Decrement?

If an increment / decrement operator is written before a variable, it is referred to as preincrement / predecrement operators and if it is written after a variable, it is referred to as post increment / postdecrement operator.

Expression     Explanation

++a                  Increment a by 1, then use the new value of a

a++                  Use value of a, then increment a by 1

–b                    Decrement b by 1, then use the new value of b

b–                    Use the current value of b, then decrement by 1

The precedence of these operators is right to left.

Example:

int a = 2, b=3,c;

c = ++a – b- -;

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

 

OUTPUT

a = 3, b = 2, c = 0.

Example2:

int a = 1, b = 2, c = 3,k;

k = (a++)*(++b) + ++a – –c;

printf(“a=%d,b=%d, c=%d, k=%d”,a,b,c,k);

 

OUTPUT

a = 3, b = 3, c = 2, k = 6

You cannot copy content of this page