I have the following code in C (I'm using tdm-gcc 4.9.1 and Netbeans 8.0.2):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * pr(char * str);
int main(void)
{
char * x;
x = pr("Ho Ho Ho!");
return 0;
}
//*************************************
char * pr(char * str)
{
char * pc;
pc = str;
while (* pc)
{
putchar(* pc++);
printf(" %d %d\n", pc, str);
}
printf("\n");
printf(" %d %d\n", pc, str);
printf("\n");
do
{
putchar(* pc--); // alternate case: * --pc
printf(" %d %d\n", pc, str);
} while (pc - str);
return (pc);
}
In the do-while loop, when the argument inside the putchar function is
* pc--
I have the following result printed (1st column prints the string "Ho Ho Ho!", one character at a time, 2nd column prints the address of pointer-to-char pc, whereas the 3rd column prints the address of pointer to char str:
H 4206629 4206628
o 4206630 4206628
4206631 4206628
H 4206632 4206628
o 4206633 4206628
4206634 4206628
H 4206635 4206628
o 4206636 4206628
! 4206637 4206628
4206637 4206628
4206636 4206628
! 4206635 4206628
o 4206634 4206628
H 4206633 4206628
4206632 4206628
o 4206631 4206628
H 4206630 4206628
4206629 4206628
o 4206628 4206628
or
Ho Ho Ho!!oH oH o
When the argument inside the putchar function is
* --pc
The corresponding result is
H 4206629 4206628
o 4206630 4206628
4206631 4206628
H 4206632 4206628
o 4206633 4206628
4206634 4206628
H 4206635 4206628
o 4206636 4206628
! 4206637 4206628
4206637 4206628
! 4206636 4206628
o 4206635 4206628
H 4206634 4206628
4206633 4206628
o 4206632 4206628
H 4206631 4206628
4206630 4206628
o 4206629 4206628
H 4206628 4206628
or
Ho Ho Ho!!oH oH oH
My question is as follows: What's the difference between the postfix and the prefix decrement operator regarding the output of the putchar function inside the do-while loop?
Any feedback would be greatly appreciated.