when running the program with 23 405, it executes without any errors.
Posted code is not true code given return 1}
, a syntax error.
Assuming that was a posting glitch and return 1;}
was coded:
Code lacks declaration of strtol()
and printf()
.
Add these to avoid undefined behavior:
#include <stdio.h>
#include <stdlib.h>
I suspect code died due to some undefined behavior, and did not print out anything. Thus OP incorrectly assumed success.
Others problems should the reported "program with 23 405" also not be quite true:
Passing an invalid pointer
Calling strtol(argv[1], &inputs, 10)
with an invalid argv[1]
is undefined behavior. Validate argv[]
first.
if (argc < 3) {
printf("Error: too few arguments.\n");
return 1;
}
Potential loss of information
strtol()
return a long
, not int
.
long input1= strtol(argv[1], &inputs, 10);
long input2= strtol(argv[2], &inputs, 10);
Lack of and incorrect no conversion detection
char *end1;
char *end2;
long input1= strtol(argv[1], &end1, 10);
long input2= strtol(argv[2], &end2, 10);
if (argv[1] == end1 || argv[2] == end2) {
printf("Error: no conversion.");
return 1;
}
Lack of affirmation success
To help distinguish success from undefined behavior, print success.
// add
printf("Success\n");
return 0;
}