Mbk has asked for the wisdom of the Perl Monks concerning the following question:
and the equivalent C program:#!/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
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,#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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Autoincrement operator precedence difference between C and Perl
by ikegami (Patriarch) on Oct 09, 2007 at 07:00 UTC | |
|
Re: Autoincrement operator precedence difference between C and Perl
by Corion (Patriarch) on Oct 09, 2007 at 07:01 UTC | |
by oha (Friar) on Oct 09, 2007 at 10:21 UTC | |
|
Re: Autoincrement operator precedence difference between C and Perl
by GrandFather (Saint) on Oct 09, 2007 at 07:06 UTC | |
|
Re: Autoincrement operator precedence difference between C and Perl
by shmem (Chancellor) on Oct 09, 2007 at 07:10 UTC | |
|
Re: Autoincrement operator precedence difference between C and Perl
by blokhead (Monsignor) on Oct 09, 2007 at 13:12 UTC | |
by shmem (Chancellor) on Oct 09, 2007 at 21:49 UTC | |
|
Re: Autoincrement operator precedence difference between C and Perl
by Mbk (Novice) on Oct 09, 2007 at 08:30 UTC |