The following program gives output:
hffltgpshfflt
Can somebody explain this how does operator precedence of postfix++,prefix++ and dereference(*) operators decide this output?
#include<stdio.h>
int main()
{
char arr[] = "geeksforgeeks";
char *ptr = arr;
while(*ptr != '\0')
++*ptr++;
printf("%s %s", arr, ptr);
getchar();
return 0;
}
It's easy once one learn the operator precedence and associativity rules.
You expression ++*ptr++
is equivalent to ++*(ptr++)
which is equivalent to ++(*(ptr++))
.
So the order of operations is
ptr
)ptr
(before the pointer is incremented)ptr
, turning e.g. 'g'
to 'h'
etc.ptr
is incremented (actually part of step 1)