C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 27)
27.
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;
}
IndiaBIX India
IndiaBIX IndiaBIX
India India
Error
Answer: Option
Explanation:

It prints 'IndiaBIX IndiaBIX' in TurboC (in 16 bit platform).

It may cause a 'segmentation fault error' in GCC (32 bit platform).

Discussion:
19 comments Page 2 of 2.

Nani said:   9 years ago
If we use char so we need to use prints("%c") right there they using %s is it correct?

Prafull said:   10 years ago
*str3 does not initialize (or) does get 0 allocated memory. So it will give error.

Lalit said:   1 decade ago
It will give segmentation fault as we are trying to write read only memory.

Kavi said:   1 decade ago
Sir in printf statement have str1 is India but the result is indiabix. How?

Rabel said:   1 decade ago
If I take the example 32bits gcc compiler, the answer should be D.

Kiran said:   1 decade ago
In 32 it causes segmentation faut.

Chatrapathi.s said:   1 decade ago
It leads to segmentation fault.

Karan said:   8 years ago
Please explain clearly.

Prashanth said:   7 years ago
Thanks @Vishal Kumar.
(1)


Post your comments here:

Your comments will be displayed after verification.