edited by
4,834 views
0 votes
0 votes
int main() 
{ 
    char *p="abc"; 
    char *q="abc123"; 
    while(*p++=*q++)
        printf("%c %c",*p,*q);
}



The Output is b bc c 1a 2b 3c

Here explain how did 1a 2b 3c will come as at that time p has null value

edited by

1 Answer

1 votes
1 votes

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.

edited by

Related questions

0 votes
0 votes
1 answer
1
Psnjit asked Jan 12, 2019
1,161 views
main(){unsigned int i= 255;char *p= &i;int j= *p;printf("%d\n", j);unsigned int k= *p;printf("%d", k);} Both the outputs are -1. I have even tried with - int i = 255(3rd ...
0 votes
0 votes
1 answer
2
Mr khan 3 asked Nov 3, 2018
1,026 views
#include<stdio.h void fun(int *p,int *q) { p=q; *p=q; } int i=0,j=1; int main() { fun(&i,&j); printf("%d%d",i,j); }What will be the output of i and j in 16-bit C Compiler...
0 votes
0 votes
0 answers
4
garvit_vijai asked Sep 2, 2018
711 views
Please explain the output for the following program: #include<stdio.h>int main() { int i = 100; int *a = &i; float *f = (float *)a; (*f)++; pri...