Instead of running the above code run the following code. This is a little modulation of your code to make your doubts clear.
int main()
{
char*p=”abc”;
char *q=”abc123”;
while(*p=*q)
{
printf(“p=%d,q=%d”,p,q);
printf(“\n”);
printf(“*p=%c,*q=%c”,*p,*q);
printf(“\n”);
*p++;
*q++;
printf(“%c %c”,*p,*q);
printf(“\n”);
}
}
Output
p=100180,q=100184
*p=a,*q=a
b b
p=100181,q=100185
*p=b,*q=b
c c
p=100182,q=100186
*p=c,*q=c
1
p=100183,q=100187
*p=1,*q=1
a 2
p=100184,q=100188
*p=2,*q=2
b 3
p=100185,q=100189
*p=3,*q=3
c
If you look at the outputs carefully you will find that at which address which values are stored
Address Value
100180 ………………………………………….. a
100181 ………………………………………….. b
100182……………………………………………. c
100183…………………………………………….. null
100184 ……………………………………………. a
100185 ……………………………………………. b
100186 …………………………………………….. c
100187 ……………………………………………… 1
100188 ……………………………………………… 2
100189 ………………………………………………. 3
Now your problem starts from where *p starts iterating its values (bold part of output) this is happening because at that time the value of p is 100184 and after that p= 100185 and after that p=100186 so, it takes the values a,b,c again.
I hope you will understand now.