Language: C


int main(int argc, char *argv[])
{
    long input;
    long result;

    
    /* Check argument num */
    if (argc != 2) {
        exit(EXIT_FAILURE);
    }
    
    input = validate_input(argv[1]);
    if (input == LONG_MAX) {
        printf("Invalid argument.\n");
        
        exit(EXIT_FAILURE);
    }

    result = input * input * input;
    
    printf("%ld\n", result);
    
    exit(EXIT_SUCCESS);
}

/*
 * 入力値をチェック
 */
long validate_input(char *input) 
{
    long val;
    char *error;
    
    if (input == NULL) {
        return LONG_MAX;
    }
    
    errno = 0;
    val = strtol(input, &error, 10);
    
    if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
            || (errno != 0 && val == 0)) {
        perror("strtol");
        return LONG_MAX;
    }

    if (*error != '\0') {
        /* Invalid value */
        return LONG_MAX;
    }

    return val;
    
}

Compiler output:
main.c: In function 'main':
main.c:10:9: error: implicit declaration of function 'exit' [-Wimplicit-function-declaration]
   10 |         exit(EXIT_FAILURE);
      |         ^~~~
main.c:1:1: note: include '<stdlib.h>' or provide a declaration of 'exit'
  +++ |+#include <stdlib.h>
    1 | 
main.c:10:9: warning: incompatible implicit declaration of built-in function 'exit' [-Wbuiltin-declaration-mismatch]
   10 |         exit(EXIT_FAILURE);
      |         ^~~~
main.c:10:9: note: include '<stdlib.h>' or provide a declaration of 'exit'
main.c:10:14: error: 'EXIT_FAILURE' undeclared (first use in this function)
   10 |         exit(EXIT_FAILURE);
      |              ^~~~~~~~~~~~
main.c:10:14: note: 'EXIT_FAILURE' is defined in header '<stdlib.h>'; this is probably fixable by adding '#include <stdlib.h>'
main.c:10:14: note: each undeclared identifier is reported only once for each function it appears in
main.c:13:13: error: implicit declaration of function 'validate_input' [-Wimplicit-function-declaration]
   13 |     input = validate_input(argv[1]);
      |             ^~~~~~~~~~~~~~
main.c:14:18: error: 'LONG_MAX' undeclared (first use in this function)
   14 |     if (input == LONG_MAX) {
      |                  ^~~~~~~~
main.c:1:1: note: 'LONG_MAX' is defined in header '<limits.h>'; this is probably fixable by adding '#include <limits.h>'
  +++ |+#include <limits.h>
    1 | 
main.c:15:9: error: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
   15 |         printf("Invalid argument.\n");
      |         ^~~~~~
main.c:1:1: note: include '<stdio.h>' or provide a declaration of 'printf'
  +++ |+#include <stdio.h>
    1 | 
main.c:15:9: warning: incompatible implicit declaration of built-in function 'printf' [-Wbuiltin-declaration-mismatch]
   15 |         printf("Invalid argument.\n");
      |         ^~~~~~
main.c:15:9: note: include '<stdio.h>' or provide a declaration of 'printf'
main.c:17:9: warning: incompatible implicit declaration of built-in function 'exit' [-Wbuiltin-declaration-mismatch]
   17 |         exit(EXIT_FAILURE);
      |         ^~~~
main.c:17:9: note: include '<stdlib.h>' or provide a declaration of 'exit'
main.c:22:5: warning: incompatible implicit declaration of built-in function 'printf' [-Wbuiltin-declaration-mismatch]
   22 |     printf("%ld\n", result);
      |     ^~~~~~
main.c:22:5: note: include '<stdio.h>' or provide a declaration of 'printf'
main.c:24:5: warning: incompatible implicit declaration of built-in function 'exit' [-Wbuiltin-declaration-mismatch]
   24 |     exit(EXIT_SUCCESS);
      |     ^~~~
main.c:24:5: note: include '<stdlib.h>' or provide a declaration of 'exit'
main.c:24:10: error: 'EXIT_SUCCESS' undeclared (first use in this function)
   24 |     exit(EXIT_SUCCESS);
      |          ^~~~~~~~~~~~
main.c:24:10: note: 'EXIT_SUCCESS' is defined in header '<stdlib.h>'; this is probably fixable by adding '#include <stdlib.h>'
main.c: At top level:
main.c:30:6: error: conflicting types for 'validate_input'; have 'long int(char *)'
   30 | long validate_input(char *input)
      |      ^~~~~~~~~~~~~~
main.c:13:13: note: previous implicit declaration of 'validate_input' with type 'int()'
   13 |     input = validate_input(argv[1]);
      |             ^~~~~~~~~~~~~~
main.c: In function 'validate_input':
main.c:35:18: error: 'NULL' undeclared (first use in this function)
   35 |     if (input == NULL) {
      |                  ^~~~
main.c:1:1: note: 'NULL' is defined in header '<stddef.h>'; this is probably fixable by adding '#include <stddef.h>'
  +++ |+#include <stddef.h>
    1 | 
main.c:36:16: error: 'LONG_MAX' undeclared (first use in this function)
   36 |         return LONG_MAX;
      |                ^~~~~~~~
main.c:36:16: note: 'LONG_MAX' is defined in header '<limits.h>'; this is probably fixable by adding '#include <limits.h>'
main.c:39:5: error: 'errno' undeclared (first use in this function)
   39 |     errno = 0;
      |     ^~~~~
main.c:1:1: note: 'errno' is defined in header '<errno.h>'; this is probably fixable by adding '#include <errno.h>'
  +++ |+#include <errno.h>
    1 | 
main.c:40:11: error: implicit declaration of function 'strtol' [-Wimplicit-function-declaration]
   40 |     val = strtol(input, &error, 10);
      |           ^~~~~~
main.c:42:19: error: 'ERANGE' undeclared (first use in this function)
   42 |     if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
      |                   ^~~~~~
main.c:42:56: error: 'LONG_MIN' undeclared (first use in this function)
   42 |     if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
      |                                                        ^~~~~~~~
main.c:42:56: note: 'LONG_MIN' is defined in header '<limits.h>'; this is probably fixable by adding '#include <limits.h>'
main.c:44:9: error: implicit declaration of function 'perror' [-Wimplicit-function-declaration]
   44 |         perror("strtol");
      |         ^~~~~~