C Programming - Strings - Discussion

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

[A]. IndiaBIX India
[B]. IndiaBIX IndiaBIX
[C]. India India
[D]. Error

Answer: Option B

Explanation:

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

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


Ashish Pathak said: (Dec 7, 2010)  
If we assume that string literals (e.g. "India", "BIX") are stored in code segment of program, then all given options of answers are incorrect. The correct option should have been run-time error or segmentation fault. Above program is trying to modify string stored in code (read-only) segment of program. Online compiler gives segmentation fault.

Ajay said: (Jan 22, 2011)  
There is no memory allocated to str3. Hence it points to null.

So the statement str3 = strcat(str1, str2); will cause a segmentation fault.

Wikiok said: (Mar 9, 2011)  
There are 2 results after the strcat line:
- str1 = str1 concatenate str2 <- segmentation fault. str1 has only 6 byte in memory.
- str3 = str1 <- str3 will point to the same memory as str1 do.

If we write char str1[8] instead of char *str1, then the code will work (tested in GCC).

Kiran said: (Apr 1, 2011)  
In 32 it causes segmentation faut.

Rabel said: (Sep 12, 2011)  
If I take the example 32bits gcc compiler, the answer should be D.

Lalit said: (Aug 15, 2012)  
It will give segmentation fault as we are trying to write read only memory.

Sachi said: (Nov 13, 2012)  
As we write char *str3; it is stored in Read only memory (code segment) in GCC.

So we can not change the content. So it gives Segmentation fault.

Rathi said: (Sep 5, 2013)  
#include <stdio.h>

char *strcat(char *dest, const char *src);

Description:

The strcat function concatenates or appends src to dest. All characters from src are copied including the terminating null character.

Return Value:

The strcat function returns dest.

Example:

#include <stdio.h>

int main() {
char string1[20];
char string2[20];

strcpy(string1, "Hello");
strcpy(string2, "Hellooo");

printf("Returned String : %s\n", strcat( string1, string2 ));
printf("Concatenated String : %s\n", string1 );

return 0;
}

It will proiduce following result:

Returned String : HelloHellooo
Concatenated String : HelloHellooo

Kavi said: (Jun 27, 2014)  
Sir in printf statement have str1 is India but the result is indiabix. How?

Vishal Kumar said: (Jan 29, 2015)  
//This works, problem was str1 was not having enough memory.

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

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

Chatrapathi.s said: (Jun 4, 2015)  
It leads to segmentation fault.

Keval Patel said: (Jun 11, 2015)  
If we assume that we are running this program on GCC compiler then for str1 and str2 pointers assigned strings will be stored in code segment of program, then all given options of answers are incorrect.

The correct option should have been run-time error or segmentation fault. Above program is trying to modify string stored in code (read-only) segment of program. Online compiler gives segmentation fault.

Razia said: (Jul 15, 2015)  
In the above program we are not using strcpy function to copy the concatenated value of s1 and s2 to s3. So I think it may result an error.

Prafull said: (Jan 4, 2016)  
*str3 does not initialize (or) does get 0 allocated memory. So it will give error.

Nani said: (Jul 6, 2016)  
If we use char so we need to use prints("%c") right there they using %s is it correct?

Karan said: (Sep 12, 2017)  
Please explain clearly.

Prashanth said: (Dec 14, 2018)  
Thanks @Vishal Kumar.

Pooja Gurav said: (May 3, 2020)  
I don't understand how you the output of program because the str3 gives us indiabix but the value of str1 is India. So how correct option is b? Please tell me.

Nitesh said: (Dec 6, 2020)  
It's all about memory nothing else.

str1 allocates 8 bytes.

But when we perform concate operation size of str1 extended. That's Why it gives error in old compilers.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.