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;
}
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.
Kavi said:
1 decade ago
Sir in printf statement have str1 is India but the result is indiabix. How?
Rathi said:
1 decade ago
#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
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
(1)
Sachi said:
1 decade ago
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.
So we can not change the content. So it gives Segmentation fault.
Lalit said:
1 decade ago
It will give segmentation fault as we are trying to write read only memory.
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.
Wikiok said:
1 decade ago
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).
- 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).
Ajay said:
1 decade ago
There is no memory allocated to str3. Hence it points to null.
So the statement str3 = strcat(str1, str2); will cause a segmentation fault.
So the statement str3 = strcat(str1, str2); will cause a segmentation fault.
Ashish Pathak said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers