0% found this document useful (0 votes)
87 views1 page

Increment and Decrement Operators

In C, the increment (++) and decrement (--) operators increase or decrease the value of a variable by 1. As unary operators, they are used on a single operand. When used as a prefix (++var or --var), the operator will increment/decrement the value first before returning it. When used as a postfix (var++ or var--), the original value is returned first before incrementing/decrementing. For example, in the code shown, c++ displays the original value of 2 and then increments c to 3, while ++c first increments c to 4 and then displays the new value.

Uploaded by

slspa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views1 page

Increment and Decrement Operators

In C, the increment (++) and decrement (--) operators increase or decrease the value of a variable by 1. As unary operators, they are used on a single operand. When used as a prefix (++var or --var), the operator will increment/decrement the value first before returning it. When used as a postfix (var++ or var--), the original value is returned first before incrementing/decrementing. For example, in the code shown, c++ displays the original value of 2 and then increments c to 3, while ++c first increments c to 4 and then displays the new value.

Uploaded by

slspa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Increment and decrement operators

In C, ++ and -- are called increment and decrement operators respectively. Both of these operators are unary
operators, i.e, used on single operand. ++ adds 1 to operand and -- subtracts 1 to operand respectively. For
example:
Let a=5 and b=10
a++;

//a becomes 6

a--;

//a becomes 5

++a;

//a becomes 6

--a;

//a becomes 5

NOTE : ++a !=

a+1, but ++a ==

a=a+1;

Difference between ++ and -- operator as postfix and prefix


When i++ is used as prefix(like: ++var ), ++var will increment the value of var and then return it but, if ++ is
used as postfix(like: var++), operator will return the value of operand first and then only increment it. This can
be demonstrated by an example:
#include <stdio.h>
int main(){
int c=2,d=2;
printf("%d\n",c++); //this statement displays 2 then, only c incremented by 1 to 3.
printf("%d",++c); //this statement increments 1 to c then, only c is displayed.
return 0;
}

Output
2
4

You might also like