Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

31.Increment/Decrement operators

 

Increment/Decrement operators:

++, -- are unary operators used to increment, decrement the value of a variable by one.

These operators are used as both prefix and postfix operators.

There may be no difference among postfix and prefix if there are no other operations are involved other than increment, decrement.

Example1:

#include<stdio.h>

Output:

11

12

11

10

 

 
void main()

{

int a=10;

 

a++;

printf("%d",a);

++a;

printf("\n%d",a);

a--;

printf("\n%d",a);

--a;

printf("\n%d",a);

 

}

 

Example explained:

According to the output we come to know that both prefix and postfix operators work similar.

 

Postfix VS prefix:

If other operations like assigning, comparing or printing are involved in increment and decrement statements then there would be difference among the prefix and postfix operators.

Postfix operator Increments or decrements the value of a variable after the completion of other operations.

Prefix operator Increments or decrements the value of a variable before performing other operations.

 

Example1:

#include<stdio.h>

void main()

{

int a=10,b,c;

 

b=a++;

c=++b;

printf("a=%d",a);

printf("\nb=%d",b);

printf("\nc=%d",c);

 

}

 

Output:

a=11

b=11

c=11

 

Example explained:

Because a++ is post increment "a" is assigned to "b" before "a" get incremented.

Because ++b is pre increment "b" is incremented before assigning to "c"

Example2:

#include<stdio.h>

void main()

{

int a=10,b=20;

 

printf("a=%d",++a);

printf("\nb=%d",b--);

 

}

 

Output:

a=11

b=20

Example explained:

Because ++a is pre increment "a" is incremented before printing

Because b-- is post decrement "b" is printed before decrement.

 

Example3:

#include<stdio.h>

void main()

{

int a=10,b;

 

b=++a;

if(a<++b)

   printf("Hello");

else

   printf("World");

 

}

 

Output:

Hello

 

Example explained:

Before checking condition both "a" and "b" are equal (11)

Because ++b is pre increment "b" is incremented before comparison 

a<++b (11<12) is true

 

Example4:

#include<stdio.h>

void main()

{

int a=10;

 

if(10<++a)

    printf("a=%d",a);

 

}

 

Output:

a=11

 

 

 

Example explained:

Because ++a is pre incrementing "a" is incremented before comparison

Because 10<++a (10<11) is true the value of "a" is printed.

 

 

 

Example5:

 

#include<stdio.h>

void main()

{

int a=10;

 

printf("%d\t%d\t%d",--a,a++,++a);

 

}

 
 

 

 

 


 

 

 

 

 

 

 


Output:

11        11        11

 

Example explained:

The printf statement argument assigning happens from right to left

Because ++a is pre increment, the value of "a" incremented before printing.

Because a++ is post increment, the value of "a" is printed before incrementing.

Because --a is pre decremented, the value of is decremented before printing.

 

Example6:

#include<stdio.h>

void main()

{

int a,b,c;

 

a=10;

printf("%d\t%d\t%d",c=++b,b=a++,--a);

 

}

 

Output:

10        9          9

 

Example explained:

Because --a is pre decrement "a" is decremented before printing ("a" is 9)

Because a++ is post increment "a" is assigned to "b" before incrementing ("b" is 9)

Because ++b is pre incrementing "b" is incremented before assigning. ("c" is 10)

 

 

0 comments:

Post a Comment