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.

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)

Riswan said:   6 years ago
Thanks to you all.

Raja said:   7 years ago
Thanks @Avi.

Manish said:   7 years ago
I think while need a condition but there is '='assignment operator that can only assign the value so. It couldn't run.

Rohan said:   7 years ago
You are absolutely right @Sowmya.

str1 value must be "India" as output.

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)

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)

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

Praju said:   9 years ago
Well done @Uttam.


Post your comments here:

Your comments will be displayed after verification.