You say that your code got AC on codechef but gets Seg. Fault on pc?
There is a reason for it. It depends on the online judge and how he handles exceptions.
Look at following code-
Now, the output of this code would vary from one compiler to error. One compiler may throw error at “cout<< arr;” as here we are going out of bounds, other compiler may assign garbage values to both, arr and arr and print those.
And compilers of sites like hackerrank are so polite, they don’t tell you if anythings wrong or not, they just assign it 0 (and make debugging a hell of another level sometimes XD). Meaning, it would print 0 for arr and arr.
It depends on how the compiler handles these things. Some very common errors for Seg. Fault/ Runtime errors on pc are-
Compilers may again differ here, since j isn’t initialised. By default (generally pc compilers), garbage value should be given and the line arr[j]==1 should throw an error (as garbage values are usually out of index/can be negative &etc.)
However, as I said, some compilers would assign j a initial value of 0, then since arr is also not initialised, they would give arr[j] also a default value of 0. And for them, your code would run without error, (and perhaps print desired output if you intended arr[j] =0).
Generally online compilers have exceptional handling, so they tolerate minor errors from our part. That’s my personal opinion of why your code ran on codechef but gave error on pc.
Also, you raised a point as such where to write codes if PC compilers give such errors. You can use code, compile and run feature of codechef, or go to any site like hackerrank, hackerearth etc. , open any practice problem and start coding your code there. Then to test it, use the option of “Test against custom input”. So you can exploit the benefits of that online compiler without any hassle.
(PS: Yes, I exploit this more times than I would like to admit.)