Machine-translated by Google. Questions are authored in English.
Top

Discussion

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;
}

 

  • A.IndiaBIX
  • B.BndiaBIdiaBIXia
  • C.India
  • D.(null)

Answer: B

Correct answer: BndiaBIdiaBIXia

The loop copies str2 into str1 one character at a time, printing the whole of str1 after each copy, and stops when the assigned value is the terminating null.

After copying B, str1 reads Bndia; after I, BIdia; after X, BIXia. The three prints concatenate to BndiaBIdiaBIXia. Note that the assignment's value is what controls the loop, which is the standard C string-copy idiom.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.