This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Pre and post increment
- From: imap at adari dot net
- To: gcc at gcc dot gnu dot org
- Date: Mon, 20 Jul 2009 11:30:31 -0400
- Subject: Pre and post increment
Hello,
Here is a program with output in gcc (4.3.2) on pre and post increments:
//--------------------code begin--------------------------------------------
#include <stdio.h>
main () {
int a;
a=1; printf ("1. %d %d\n", ++a, a); // 1. 2 2
a=1; printf ("2. %d %d\n", a, a++); // 2. 2 1
a=1; printf ("3. %d %d\n", a++, a); // 3. 1 2
a=1; printf ("4. %d %d\n", a++, ++a); // 4. 2 3
a=1; printf ("5. %d %d\n", ++a, a++); // 5. 3 1
a=1; printf ("6. %d %d %d\n", ++a, a, a++); // 6. 3 3 1
a=1; printf ("7. %d %d %d\n", a++, a, ++a); // 7. 2 3 3
a=1; printf ("8. %d %d %d %d\n", a, a++, ++a, a); // 8. 3 2 3 3
a=1; printf ("9. %d %d %d %d\n", a, ++a, a++, a); // 9. 3 3 1 3
a=1; printf ("10. %d %d %d %d %d\n", a, a++, a, ++a, a);// 10. 3 2 3 3 3
a=1; printf ("11. %d %d %d %d %d\n", a, ++a, a, a++, a);// 11. 3 3 3 1 3
}
//--------------------code end--------------------------------------------
The output from the program is listed next to it in comments. I thought
I knew something about pre and post increments, but this program busted
my understanding of the pre and post increments. I would appreciate
if someone could explain me the output.
Thanks