Ambiguity in Pre & Post increment - C Programming

42 views Asked by At

Writing a simple C program to understnad pre and post increment. I could understant case 1 and 3 but not getting how the value of i becoming 15 and 16 in case 2 and 4 resp. Kindly share your thoughts about case 2 and 4.

#include <stdio.h>

int main() {

int i , j=6 ;
i=j++ + j++ ;
printf("Case 1 : With j=6 and i=j++ + j++ : i %d j %d\n",i,j);

j=6;
i=++j + j++ ;
printf("Case 2 : With j=6 and i=++j + j++ : i %d j %d\n",i,j);

j=6;
i=j++ + ++j ;
printf("Case 3 : With j=6 and i=j++ + ++j : i %d j %d\n",i,j);

j=6;
i=++j + ++j ;
printf("Case 4 : With j=6 and i=++j + ++j : i %d j %d\n",i,j);

    return 0;
}

This is the output that I'm getting for the above code.

OUTPUT :

Case 1 : With j=6 and i=j++ + j++ : i 13 j 8
Case 2 : With j=6 and i=++j + j++ : i 15 j 8
Case 3 : With j=6 and i=j++ + ++j : i 14 j 8
Case 4 : With j=6 and i=++j + ++j : i 16 j 8

Thanks in advance.

I could understant case 1 and 3 but not getting how the value of i becoming 15 and 16 in case 2 and 4 resp. Kindly share your thoughts about case 2 and 4.

0

There are 0 answers