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

Discussion

What will be the output of the program (Turbo C in 16 bit platform DOS) ?

#include <stdio.h>
#include <string.h>

int main()
{
    char *str1 = "India";
    char *str2 = "BIX";
    char *str3;
    str3 = strcat(str1, str2);
    printf("%s %s\n", str3, str1);
    return 0;
}

 

  • A.IndiaBIX India
  • B.IndiaBIX IndiaBIX
  • C.India India
  • D.Error

Answer: B

Correct answer: IndiaBIX IndiaBIX

strcat appends to its first argument and returns that same pointer, so str3 and str1 end up pointing at the same characters - and both print IndiaBIX. Note that str1 points to a string literal with no room after it, so appending to it is undefined behaviour; the old DOS compiler this question assumes allowed it, but a modern one will usually crash.

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

Post your comment

Please log in or register to post a comment.