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 1 of 2.
Pooja gurav said:
5 years ago
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.
(2)
Prashanth said:
7 years ago
Thanks @Vishal Kumar.
(1)
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)
Vishal Kumar said:
1 decade ago
//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;
}
#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;
}
(1)
Nitesh said:
5 years ago
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.
str1 allocates 8 bytes.
But when we perform concate operation size of str1 extended. That's Why it gives error in old compilers.
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.
Karan said:
8 years ago
Please explain clearly.
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.
Razia said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers