You are not logged in. Please login at www.codechef.com to post your questions!

×

why i got runtime error

include<stdio.h>

include<string.h>

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

asked 17 Aug '15, 12:47

sumit_suthar's gravatar image

2★sumit_suthar
528
accept rate: 0%


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

link

answered 17 Aug '15, 13:25

prrateekk's gravatar image

3★prrateekk
534216
accept rate: 12%

edited 17 Aug '15, 13:27

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

eg. string s="hello";

link

answered 17 Aug '15, 23:46

admin123's gravatar image

5★admin123
1.2k12
accept rate: 28%

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?

link

answered 18 Aug '15, 00:22

adi28galaxyak's gravatar image

5★adi28galaxyak
1164
accept rate: 14%

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

It will work. :)

link

answered 18 Aug '15, 19:04

bradley's gravatar image

3★bradley
6562321
accept rate: 20%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×643
×62

question asked: 17 Aug '15, 12:47

question was seen: 1,606 times

last updated: 18 Aug '15, 19:04