Code works in Geany IDE, but segmentation fault in HackerRanks. What's the technical reason for this?












0















It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;

scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}









share|improve this question


















  • 1





    I bet more than 100 chars in the input. And you write outside the bounds

    – P__J__
    Jan 1 at 20:03






  • 1





    Are you sure a newline will terminate the input? Check for EOF as well, but you must change char ch; to int ch; I would use while ((ch = getchar()) != 'n' && ch != EOF) Without a newline, you'll break the array.

    – Weather Vane
    Jan 1 at 20:04








  • 1





    I mean "exceed the bounds of the array c" since without a newline the loop continues until you break something.

    – Weather Vane
    Jan 1 at 20:09








  • 2





    Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.

    – Weather Vane
    Jan 1 at 20:10






  • 2





    Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.

    – n.m.
    Jan 1 at 20:27
















0















It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;

scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}









share|improve this question


















  • 1





    I bet more than 100 chars in the input. And you write outside the bounds

    – P__J__
    Jan 1 at 20:03






  • 1





    Are you sure a newline will terminate the input? Check for EOF as well, but you must change char ch; to int ch; I would use while ((ch = getchar()) != 'n' && ch != EOF) Without a newline, you'll break the array.

    – Weather Vane
    Jan 1 at 20:04








  • 1





    I mean "exceed the bounds of the array c" since without a newline the loop continues until you break something.

    – Weather Vane
    Jan 1 at 20:09








  • 2





    Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.

    – Weather Vane
    Jan 1 at 20:10






  • 2





    Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.

    – n.m.
    Jan 1 at 20:27














0












0








0








It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;

scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}









share|improve this question














It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?



#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;

scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}






c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 19:58









brunshtebrunshte

236




236








  • 1





    I bet more than 100 chars in the input. And you write outside the bounds

    – P__J__
    Jan 1 at 20:03






  • 1





    Are you sure a newline will terminate the input? Check for EOF as well, but you must change char ch; to int ch; I would use while ((ch = getchar()) != 'n' && ch != EOF) Without a newline, you'll break the array.

    – Weather Vane
    Jan 1 at 20:04








  • 1





    I mean "exceed the bounds of the array c" since without a newline the loop continues until you break something.

    – Weather Vane
    Jan 1 at 20:09








  • 2





    Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.

    – Weather Vane
    Jan 1 at 20:10






  • 2





    Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.

    – n.m.
    Jan 1 at 20:27














  • 1





    I bet more than 100 chars in the input. And you write outside the bounds

    – P__J__
    Jan 1 at 20:03






  • 1





    Are you sure a newline will terminate the input? Check for EOF as well, but you must change char ch; to int ch; I would use while ((ch = getchar()) != 'n' && ch != EOF) Without a newline, you'll break the array.

    – Weather Vane
    Jan 1 at 20:04








  • 1





    I mean "exceed the bounds of the array c" since without a newline the loop continues until you break something.

    – Weather Vane
    Jan 1 at 20:09








  • 2





    Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.

    – Weather Vane
    Jan 1 at 20:10






  • 2





    Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.

    – n.m.
    Jan 1 at 20:27








1




1





I bet more than 100 chars in the input. And you write outside the bounds

– P__J__
Jan 1 at 20:03





I bet more than 100 chars in the input. And you write outside the bounds

– P__J__
Jan 1 at 20:03




1




1





Are you sure a newline will terminate the input? Check for EOF as well, but you must change char ch; to int ch; I would use while ((ch = getchar()) != 'n' && ch != EOF) Without a newline, you'll break the array.

– Weather Vane
Jan 1 at 20:04







Are you sure a newline will terminate the input? Check for EOF as well, but you must change char ch; to int ch; I would use while ((ch = getchar()) != 'n' && ch != EOF) Without a newline, you'll break the array.

– Weather Vane
Jan 1 at 20:04






1




1





I mean "exceed the bounds of the array c" since without a newline the loop continues until you break something.

– Weather Vane
Jan 1 at 20:09







I mean "exceed the bounds of the array c" since without a newline the loop continues until you break something.

– Weather Vane
Jan 1 at 20:09






2




2





Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.

– Weather Vane
Jan 1 at 20:10





Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.

– Weather Vane
Jan 1 at 20:10




2




2





Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.

– n.m.
Jan 1 at 20:27





Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.

– n.m.
Jan 1 at 20:27












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998521%2fcode-works-in-geany-ide-but-segmentation-fault-in-hackerranks-whats-the-techn%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998521%2fcode-works-in-geany-ide-but-segmentation-fault-in-hackerranks-whats-the-techn%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith