Pankaj Suryawanshi
Pankaj Suryawanshi

Reputation: 743

unsigned char, why output is 254 expected is 255

#include<stdio.h> // header file
int main()
{
unsigned char a=255;
unsigned char b=0;
unsigned char c;
c=a++ + --b; // a++ is 0 and --b is 255;
printf("result=%d\n",c);
return 0;
}

output : result=254

confuse why output is not 255,how 254 ?.

Please let me know if i missed anything ?

Upvotes: 0

Views: 588

Answers (1)

Kon
Kon

Reputation: 4099

a++ is a postfix operator, meaning it will be evaluated after a is used. If you try ++a, you'll get what you expect.

If you break down c=a++ + --b; you effectively get this:

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

Upvotes: 4

Related Questions