'Why I get this error on Hackerrank in C language (~ no response on stdout ~)
Input
12
4.0
is the best place to learn and practice coding!
Output
~ no response on stdout ~
Expected output
16, 4, HackerRank is the best place to learn and practice coding!
I am getting this error when I tried scanf or fgets and I could not find a solution for it.
*** buffer overflow detected ***: ./Solution terminated
Reading symbols from Solution...done.
[New LWP 1597965]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGABRT, Aborted.
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s[] = "HackerRank ";
int j;
double k;
char g[100];
scanf(" %d %lf\n%[^\n]", &j, &k);
fgets(g,100,stdin);
printf("%d\n",(i+j));
printf("%.1lf\n",(d+k));
printf("%s\n",strcat(s, g));
// Declare second integer, double, and String variables.
// Read and save an integer, double, and String to your variables.
// Print the sum of both integer variables on a new line.
// Print the sum of the double variables on a new line.
// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
return 0;
}
Solution 1:[1]
You have multiple severe issues in your code.
If I feed your code into GCC I get valuable hints about some of them.
Besides warnings about imcompatible implicit function declarations (because you do not include stdio.h) there is:
test.c: In function ‘main’:
test.c:14:24: warning: format ‘%[^
’ expects a matching ‘char *’ argument [-Wformat=]
14 | scanf(" %d %lf\n%[^\n]", &j, &k);
| ~~~^~
| |
| char *
And I even did not add any flag to increase warning level to GCC.
You provide 3 format specifiers but only 2 addresses to store the result. This will result in taking the (more or less) random content of stack or registers and interpret it as address to store a string. This is very likey the reason for your crash.
You use fgets in the next line. Therefore I assume this is just a remains of your first attempts to read input.
As a fix, just remove that format specifier.
Then you want to concatenate your input string to some predefined string:
strcat(s, g)
This causes undefined behaviour because s cannot hold any more characters.
You must provide an array that is large enough to hold the result.
You should also check the return value of scanf and other I/O functions.
A fixed version of your code looks like this:
#include <string.h>
#include <stdio.h>
int main() {
int i = 4;
double d = 4.0;
char s[] = "HackerRank ";
int j;
double k;
char g[100];
int params = scanf(" %d %lf\n", &j, &k);
if (params != 2)
{
fprintf(stderr, "Wrong input\n");
return 1;
}
fgets(g,100,stdin);
printf("%d\n",(i+j));
printf("%.1lf\n",(d+k));
size_t len = strlen(s) + strlen(g) + 1;
char result[len];
strcpy(result, s);
strcat(result, g);
printf("%s\n", result);
return 0;
}
This fixes the crash.
Then you have wrong expected output.
16, 4, HackerRank is the best place to learn and practice coding!
You take 4.0 as input and add a fixed value which is also 4.0. That is 8.0. Not 4.
Besides these issues I would suggest not to mix scanf and fgets but to use fgets for all the input and then use sscanf to do the parsing of numbers etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gerhardh |
