why i got runtime error

pointers
string

#1

#include<stdio.h>
#include<string.h>
int main()
{
char *s1=“Hello”;
char *s2=“world”;
printf("%s",strcat(s1,s2));
return 0;
}


#2

You tried to store an entire string in a single character sized data space.

What you need to do is use a character array.

Just place square brackets([]) after s1 and s2.

#include<stdio.h>
#include<string.h>
int main(){
     char s1[]="Hello";
     char s2[]="World";
     printf("%s",strcat(s1,s2));
     return 0;
}

This will present the output as-
HelloWorld


#3

if you are using c++ then you can include < string > and use it or use character array

eg. string s=“hello”;


#4

You have written a program to print Hello world, but you are storing a Strings in characters s1 & s2.
This gives you a runtime error (RTE).

To overcome this in C can try any one of these–

int main(){

   char *s1 = "Hello", *s2 = "World";
   char v[50];
   strcat(v,s1);
   strcat(v,s2);
   printf("%s",v);

}

int main(){

 char s1[] = "Hello" , s2[] =" World";
 printf("%s",strcat(s1,s2));

}

Also read link:this – What is the difference between these steps?


#5

int main() {
char s1[]=“Hello”;
char s2[]=“world”;
printf("%s",strcat(s1,s2));
return 0;
}

It will work. :slight_smile: