-2
  1. When I submit this code on URI Online judge, they show me runtime error. I have no idea about runtime error. Please someone can explain that

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

int main(int argc, char const *argv[])
{
    double x1, y1, x2, y2, result;
    scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);

    result = sqrt(pow(x2 - x1, 2) + pow(y2 - y1 , 2));

    printf("%.4lf\n", result);
}

2 Answers2

1

The site says "ANSI C" which is a commonly used misnomer for the C89/C90 standard revision... It is the obsolete deprecated version that no one should use anywhere - it does not accept for example // comment characters.

The thing is that the implicit return 0 was added in C99. In C89 if main does not return a value, behaviour is undefined. It seems that the solution is not accepted if main does not explicitly return 0. URI Online Judge uses GCC 4.8.5. GCC 4.8.5 defaults to gnu89 "dialect" and without explicit return from main the exit code is completely random. On the other hand calling it Ansi C is not appropriate either as it still does support things like // for comments.

Thus:

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

int main(int argc, char const *argv[])
{
    double x1, y1, x2, y2, result;
    scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);

    result = sqrt(pow(x2 - x1, 2) + pow(y2 - y1 , 2));

    printf("%.4lf\n", result);
    return 0;
}

works but

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

int main(int argc, char const *argv[])
{
    double x1, y1, x2, y2, result;
    scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);

    result = sqrt(pow(x2 - x1, 2) + pow(y2 - y1 , 2));

    printf("%.4lf\n", result);
    return 42;
}

gives "Runtime error".


Another thing C89 does not accept either is %lf to signify a double for printf - l is valid for only d, i , o, u, x, and X conversion specifiers. Notably, f, is not listed! However either one works in URI, because glibc has for long supported C99.

0

In some old C specification, values returned from the process will be indeterminate when returned from the function main without using return statement. (0 will be returned in C99 or later).

Also there are online judge systems that treat being returned non-zero from the process as runtime error.

Add return 0; after the printf statement to avoid this error.

Also using %lf in printf is not allowed (invokes undefined behavior) in old C. Use %f (%.4f in this case) instead. (Using %lf for reading double in scanf is good)

MikeCAT
  • 73,922
  • 11
  • 45
  • 70