# Increment /Decrement operators in C

## Brief Explanation about Increment /Decrement operator with Examples

• In implementation, when we require to change the initial value of the variable by 1, then go for increment/decrement operators. I.e “++,--“
• When we are working with increment / decrement operator the difference b/w existing value and new value is +1 and -1 only.
• Depending on the position, these operators are classified into two types.

i.e pre & post operators.

• When the symbol is available before the operand, then it is called pre-operator, if the symbol is available after the operand, then it is called post operator
• When we are working with pre operators, data need to be modified before evaluating the expression
• When we are working with post operators, data need to be modified after evaluating the expression

### Syntax :-

First increment the value of “a” by 1 and then evaluate the expression i.e, b=1;

Int a,b;

A=1;

### Syntax1:

b=++a; pre increment

i.e b=a;

o/p: a=2  b=2

• First evaluate the expression and then increment the value of “ a “ by 1

### Syntax 2 :-

b=a++, post increment

o/p: a=2    b=1

• First decrement the value of “a” by 1 and then evaluate the expression

### Syntax 3 : -

b=-a; pre decrement

o/p : a=0 b=0.

• First evaluate the expression , later decrement the value of a by 1

### Syntax 4:

b=a - -, post decrement

B=1, a=0

• Void main()

{

Int a;

A=10;

++a; || a=a+1;

Printf(“a=%d”,a);

}

o/p: a=11

• void main()

{

Int a;

A=10;

A++; || a=a+1;

Printf(“a=%d”,a);

}

o/p: a=11

• Until we are not assigning the data to any other variable, there is no difference b/w pre and post operators.

• Void main() a=++a   +   ++a  +  ++a

{                                                        a=a+a+a

Int a;                                                 a=4+4+4

A=1;                                        all should be evaluated at a time

A=++a + ++a + ++a;

Printf("a=%d”,a);

}

o/p: 12

 (  ) +,-,!,++,-- pre *,/,% +,- <,>,<=,>= ==,!= && || ?: = ++,--,post

• ### Void main()

a=++a + a++   +     ++a

{                                                          a= a+a+a

Int a;                                                   3+3+3

A=1;                                                       9

A=++a +a ++ + ++a;

Printf(“a2%d”,a);

}

A=10;

• ### Void main()

{

Int a;

A=1;                                                  a+a+a

A=a++   +   ++a   +  a++;                   2+2+2         a=6

Printf(“a=%d”,a);                             =6     a++ =7

o/p: a=8;                                          and a++ - 7+1=8

• ### void main() a b

{                                                                 10   11     a=a++  +  ++b

Int a,b;                                                             a=a+b

A=b=10;                                                            a=10+11

A=a++   +    ++b;                                               a=21    a++=a22

B=++a    +   b++;                                                a=23  b=11

Printf ("a=%d b=%d, a, b”);                             b=++a   +  b++

}                                                                           23+11

B=34       b++

B=35.

• ### Void main()

{                                                         a=1   b=3

Int a,b;                                           a=a-b+a

A=1; b=3;                                       a=1 -2+2

A=a++ - - - b+   ++a;                        a=1

B=++b + - -a   - a++;               b=b+ a –a

3+1-1

B=3+1=4

A       b                                   a=3 2 b= 2 3

1 3    3 2                                 b=++b  + - -a  -a++

A=a++ -  - -b+  ++a                  b+a-a

A=a-b+a                                  3+2-2

2-2+2                                  b=3

A=2

++a=3

• ### Void main() a       b       c

{                                               12     2 1    3 2

Int a,b,c;                                 a=a-b+c

A=1;b=2;c=3;                              2-2+2

A=++a   -b - - + - -c;                a=2

B=a -- + b++ -c- -;                    a=1

C=a++ - ++b  +  c++;

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

2 1    1 2    2 1

A=2

A=1

A       b       c

2 1    1 2    2 1

a        b       c

1       2       1                           b=a- -  +b++ -c - -

2       3       0                                a+b-c

2+1-2

C=a++   - ++b   +   c++

a – b + c

1-3+1

C=-1

C+2=-1+1

A=2,  b=3,  c=0

• ### Void main()

{

Int a,b,c;

A=1;b=2;c=3;

a=++a – b- - + - - c;

b=a- -  + b++ - c- -;

c=a++ - ++b  + c++;

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

a        b       c                           b=a- - + b++  - c- -

1 2    2 3    1 0                           a+b-c

C=a++  - ++b  +  c++;                  2+1-2

a-b+c

1-3+1

C=-1

A=2,b=3,c=0

• ### Void main() a b       c

{                                                        2 3    4 3    6 7

Int a,b,c;                                         a= a – b + c

A=2 , b=4.  C=6;                                   33  + 6

A=++a  -  - -b + c++;                            a=6

B=a++ -b+++ - - c;                              a=6 7        b=3   c=7 6

C= - -a + ++b -++c;

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

a –  b  +    c

6 -  3  +    6

A                 b                 c

7 6              10               6 10

C   =  a  +   b   – 7

6  + 11   -7

C =  9                   c++=10

A=6             b=11           c=10

• ### Void main()

{

Int a;

A=10;

Printf(“%d   %d   %d”, ++a, a++,  ++a );

}

o/p: 13 11 11

• When we are working with printf() f’n always it executes towards from right to left because it work with the help of stack.
• In printf f’n data need to be pass towards from right to left and data need to be point towards from left to right

• ### Void main()

{

Int a;

A=100;

Printf(“ %d %d      %d   ”, a++,++a,a++);

Printf(“\na=%d”,++a);

}

o/p : 102   102   100

a=104

• ### void main()

{

Int a;

A=5;

Printf(“%d %d  %d”, ++a,++a,++a);

}

o/p: 8 7 6

• ### void main()

{

Int  a;

A=10;

Printf(“%d  %d  %d”, a++,a++,a++);

Printf(“\n a=%d”,a);

o/p: 12 11 10

a=13

• ### void main()

{

Int a;

A=15;

Printf(“%d  %d  %d”,++a,++a);

}

• ### Void main()

{

Int a;

A=5;

Printf(“%d  %d  %d”, ++a,a=10,++a);

o/p: 11   10   6

• ### void main()

{

Int a,b;

A=1;

B=++a * ++a * ++a;

Printf(“a=%d b=%d”,a,b);

• ### Void main()

{

Int a,b;

a=1;

b=++a*a++*++a;

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

}

b=a*a*a

b=3*3*3

b=27

a=4

• ### void main()

{

Int a,b;

A=1;

B=a++ - ++a  *a++;

Printf(“a=%d b=%d”,a,b);

}

A=4,b=8

• ### Void main()

{

Int a,b;

A=1;

B=a++ - a++  * a++;

Printf(“a=%d b=%d”,a,b);

}

o/p : a=4, b=1

b=a*a*a

a   1*1*1

b=1

a=4

• ### void main()

{

Int a;

A=1;

Printf(“\n%d”,++a * ++a * ++a); // case1

A=1;

Printf(“\n%d”,++a * a++ * ++a);  //case2

A=1;

Printf(“/n%d”,a++ * ++a * a++);  //case 3

A=1;

Printf(“\n%d”,a++ * a++ * a++); // case 4

A=1;

Printf(“\n%d”, ++a * a++ * a++); // case 5

A=1;

Printf(“\n%d”,a++ * a++ * ++a); // case 6

A=1;

Printf(“\n %d”,++a*++a*a++); // case 7

}

Case1 : 2*3*4=24                  case 5         2*2*3=12

Case2: 2*2*4=16                   case 6         1*2*4=8

Case 3: 1*3*3=9          case7:        2*3*3 =18

Case 4: 1*2*3=6

• Generally the expressions can be evaluated in two locations. Ie register & stack
• When we are working with register evaluation, then it works according to the priority. Ie pre-operator having highest priority than post operator.
• In register evaluation, data need to be substituted after modifying all pre value only.
• In stack evaluation, pre & post operators both contain same priority and data need to substituted at the time of evaluation the expression.
• When we are placing an expression outside of points then it should be evaluated according to register, within the printf should be evaluate according to stack.

Program:

Void main()

{

Int a,b;

A=b=1;

Printf(“\n %d %d”, ++a * ++b, a++ * b++);

A=b=2;

Printf(“\n %d %d”, ++a + b++ a++ , +  ++b );

Printf(“\n %d  %d”, - -a * ++b, ++a * - -b);

}

• When we are passing more than one expression in printf then data need to be evaluated towards from right to left, if single argument is there then data need to be substituted towards from left to right.

• ### Void main()

{

Int a;

A=printf(“welcome”);

Printf(“\n a=%d”,a);

}

o/p:  welcome

a=7

• ### void main()

{

Int a;

A=printf(“%d hello %d”,1-,20);

Printf(“\na=%d”,a);

}

When we are working with printf f’n it returns an integer value i.e total number of characters printed on console.

• ### Void main()

{

Int a;

A=printf(“ %d Hello %d”,10,200);

Printf(“\n a=%d”,a);

}

o/p: 10 Hello 200

a=12(2(0)+1(5)+5(Hello)+1(5)+3(200))

• ### void main()

{

Int a;

A=printf(“\n welcome %d”);

Printf(“NarreshIT”);

Printf(“\n a=%d ”,a);

}

o/p :  Naresh IT

welcome 9

a=w(1(\n) ++ (welcome +1(s) + 1(0));

According to behaviour of the printf always it executes towards from right to left thats why when we are working with nested printf towards from right side first printf always executes first

• ### void main()

{

Int a;

a= printf(“three %d \n”);

printf(“two %d \n”);

printf(“one %d \n”);

printf(“a=%d”,a);

}

o/p: one  two  three  a=7

• ### void main()

{

int a;

a= printf(“one\n”) + printf(“ two \n”) + printf (“three\n”);

printf(“a=%d”,a);

}

o/p : one two three

a=14

• ### void main()

{

int a;

a=printf(“\n one”) + printf(“\n two”) * printf(“\n three”);

printf(“a=%d”,a);

o/p: two three one

• ### void main()

{

int a;

a=2<5 ! printf(“NIT”);

printf(“welcome”);

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

}

o/p : NIT

a=3

• ### void main()

{

int a;

a= printf(“wi”) ? printf(“BIF”) : printf(“welcome”);

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

}

o/p: Hi bye

a=3;

• ### void main()

{

int a;

a= printf(“Hi”);

printf(“BYE”);

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

}

o/p: HIBYE Hello

a=5;

• ### void main ()

{

int a,b,c;

c=scanf(“%d%d”, &a, &b);

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

}

// i/p values are 100 200

o/p : a=100 b=200 c=2

• when we are working with scanf f’n it returns an integer value i.e total no.of i/p values provided by user.

• ### void main()

{

int a,b,c;

a=b=100;

c=scanf("%d %d %d”,a,&b);

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

}

//ip values are 500 600

o/p : a=100 b=600 c=2

• The complete behaviour of the scanf will depends on format specifier only not on argument list.
• As a programmer it is our responsibility to store the data properly by using ‘&’ symbol
• In scanf statement when we are not providing ‘&’ symbol for any variable then it will not update with new value.

• ### void main()

{

int a,b,c;

a=10; b=20;

c=scanf(“%d %d  %d”);

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

}

• ### Void main ()

{

Int a,b;

A==200*200/200;

B=200/200*200;

Printf(“a=%d b=%d”,a,b);

}

B=200/200*200

1*200

B=200

200*200=40,000

Range of integer =32767

40000                           -32767

-32767                          7232

7233                              -25536

A=-25536/200;

=-25536 *10 ^2= -12768* 10^2 =-127.68  a=-127.

2

60000                           -32768

-32767                          27232

27233                            -5536    a=-5536/300 =-18.45=-18

• ### Viod main()

{

Int a;

A=32767;

If (++a<32767)//-32768<32767

Printf(“ welcome %d”,a);

Else

Printf(“hello %d”,a);

}

O/p : welcome-36768

• ### Void main()

{

Inta;

A=-32768;

If(--a>-32768)

Printf(“welcome %d”,a);

Else

Printf(“Hello %d”,a);

}

O/p: Welcome +32767

• ### void main()

{

Int a;

A=32767;

If(a++>=32767)|| 32767>32767

Printf(“welcome %d”,a);

Else

Printf (“hello %d”,a);

}

O/p : Hello -32768

• ### Void main()

Inta;

A=-32768;

If (aà-32768)

Printf(“welcome %d:,a);

Else

Printf(“Hello %d”,a);

}

O/p:  Hello -32767.