Hi All,
I find some difference in auto increment operator precedence between C and Perl. As almost say that for Perl, the operators derived from C has the same precedence, this is somewhat different.
Here is the sample code:
Perl:
#!/usr/local/bin/perl -W
$i = 2;
$j = 10;
$k = $i++ - $i++;
print $k,"\n";
$i = 2;
$j = 10;
$k = ++$i - ++$i;
print $k,"\n";
$i = 2;
$j = 10;
$k = $i++ + $i++;
print $k,"\n";
$i = 2;
$j = 10;
$k = ++$i + ++$i;
print $k,"\n";
which outputs:
-1
0
5
8
and the equivalent C program:
#include <stdio.h>
main()
{
int i,j,k;
i = 2;
j = 10;
k = i++ - i++;
printf("i=%d, j = %d, k = %d\n",i,j,k);
i = 2;
j = 10;
k = ++i - ++i;
printf("i=%d, j = %d, k = %d\n",i,j,k);
i = 2;
j = 10;
k = i++ + i++;
printf("i=%d, j = %d, k = %d\n",i,j,k);
i = 2;
j = 10;
k = ++i + ++i;
printf("i=%d, j = %d, k = %d\n",i,j,k);
}
which outputs:
k = 0
k = 0
k = 4
k = 8
as you can observe the difference in outputs.
Could anyone help me explaining whats happening here .
why is the output different different especially with post-increment operator?
Thanks,