Understanding post and pre increment with comma in between [duplicate]

  • A+

Consider the below program

#include <stdio.h> void main(){     int p = -8;     int i = (p++, ++p);     printf("%d/n", i); } 

I am unable to get why the output is -6.

p++ would increment after the assignment statement is executed, ++p will increment before thereby making -8 to -7.

How i is assigned -6?


Because for a comma operator A,B then A is done first then B, and p++ increment the p, and ++p also increment the p. Operator precedence.

Or think like this

int i = (p++, ++p); 


p++; int i = ++p; 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: