C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 20)
20.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    char str1[] = "India";
    char str2[] = "BIX";
    char *s1 = str1, *s2=str2;
    while(*s1++ = *s2++)
        printf("%s", str1);

    printf("\n");
    return 0;
}
IndiaBIX
BndiaBIdiaBIXia
India
(null)
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
43 comments Page 1 of 5.

Mahendra said:   9 years ago
-------------------------------------------------
char str1[] = "India";
char str2[] = "BIX";
char *s1 = str1, *s2=str2;
-------------------------------------------------
Here str1 is array holding values as - >>
str1 ['I','n','d','i','a']
0 1 2 3 4
^
|
s1 // s1 is pointing to base address or
first element of array str1
And str2 is array holding values as - >>
str1 ['B','I','X']
0 1 2
^
|
s2 // s2 is pointing to base address or
first element of array str2
-------------------------------------------------
while(*s1++ = *s2++)
printf("%s", str1);
-------------------------------------------------

This while loop will be executed till there is some values in the array pointed by s1 and s2. or simply till '\0' is not encountered.

*s1++ = *s2++

The above statement means assign the value pointed by s2 into value pointed by s1;
Increment will be done after assignment because it is postfix operator.

As above statement is executed value pointed by s2(i.e. B ) is assigned to the location pointed by s1(i.e. atr1[0]).

str1 is having values as [B,n,d,i,a].
printf will print ---------->> Bndia.

On the second iteration.

As s1 and s2 are incremented they are now pointing to'n' and 'I' respectively.
Now in a similar way, and will be replaced by I.
And,

str1 is having values as [B,I,d,i,a]
printf will print ---------->> BIdia

Similarly on 3rd iteration,

str1 is having values as [B,I,X,i,a]
printf will print ---------->> BIXia

Therefore, the output is BndiaBIdiaBIXia.
(11)

Fatma mofreh said:   2 years ago
First step:
*s1++ point to I and *s2++ point to B
the first while loop I is replaced by B
so the first output will be Bndia

Second step:
*s1++ point to n and *s2++ point to I
The second while loop n is replaced by I
So the second output will be BIdia.

Third step:
*s1++ point to d and *s2++ point to X
The third while loop d is replaced by X.
So the third output will be BIXia.

Then;
*s2++ point to null so while is ended.
then print \n
(7)

Suraj said:   8 years ago
The 1st letter of *s2++ is BIX = 'B'.

'B' is assigned to the 1st letter of S1 so it becomes Bndia.

After that *S2++ mens increment by +1 so the result is BI now replace with *S1++ first two letters are BIdia(dia) from S1 we assign S2 in S1 .after that increment *S2++ so the result is BIX and it replaced 3 letter with S1 and then print.

Hope you understand.
(2)

Sowmya said:   8 years ago
Hello friends,

str1 values r stored in S1 , we r operating on S1, str1 values remain same kW,in pf statement we should print the values of str1, so str1 values should be "India " kw.
(1)

SHIVANI said:   8 years ago
Thank you all.
(1)

Emanuel said:   9 years ago
I think it will be BIX.
(1)

Shabana said:   1 decade ago
Here the str1 = "India".

str2 = "BIX".

s1=*str1,*s2=*str2 //means s1,s2 contains the the address of str1 and str2;

While conditions use the loop repetitions take place
First str1++ means s1 points to I.
srt2++ means s2 points to B.

So str1++ gets copy the value of B and replaced I in place B and then incremented it will print Bndia,

Because of loop it will points str1 to n and str2 points to I so str1 copy the I in and replace I in place of n.

And then incremented it will print --BIdia same for BIXia.

str2 did not contain any character so loop is terminated and it will print: Bndia BIdia BIXia.

Hope understand clearly.
(1)

Pradeep said:   1 decade ago
We have str1="India" and str2="BIX".
And also *s1=str1, *s2=str2.

As the given while condition is like while (*s1++==*s2++).

Here *s1 is pointing to "India" as it increment *s1++ then it.
Will point to "ndia" and similarly *s2++ will point to "IX".

While (*s1++=*s2++).

Note that it applying assignment operator not equality operator so the first letter of str1 will b replace by the first letter of str2. It will replaces till the strings completes.

So in the first iteration the output will be "Bndia".

And similarly second iteration fist two letter are replace ("In" of str1 replaces with "BI" of str2) and output generated will be BIdia. And the final Iteration output will be "BIXia".

So the actual output is "BndiaBIdiaBIXia".

Abc said:   1 decade ago
The statement in while i.e. while(*s1++=*s2++)
Assigns value at present location of s1 to present location of s2. Thereby substituting b at first position. Same process is continued

Balasaheb Mule said:   1 decade ago
s1[0]=s2[0] i.e. I replaces with B;
In 1st Loop string prints "Bndia"

Again, s1 and s2 incremented by 1.

So,that becomes,
s1[1]=s2[1] i.e. n replaces with I;
In 2nd loop string prints "BIdia"


Again, s1 and s2 incremented by 1.

So,that becomes,
s1[2]=s2[2] i.e. d replaces with X;
In 3nd loop string prints "BIXia"

And Loops terminates.

Finally output will be,
BndiaBIdiaBIXia


Post your comments here:

Your comments will be displayed after verification.