The atoi() function is a truly ancient thing. People were hunting dinner with spears when this thing first appeared, ...way before C. In C if you "abuse" this, there aren't any warnings. It is the user's responsibility to check the input values before calling atoi(). Some cases and normal ways to check for this are shown below.
char x = "2abc";
int result = atoi(x);
printf ("case a) string %s is %d as decimal\n",x,result);
if (strlen(x) == 0 || (strspn(x,digits) != strlen(x)))
printf ("case b) %s is NOT a simple positive integer!\n"
" there are either no digits or non-digits\n", x);
char y = "";
int result_y = atoi(y);
printf ("case c) a null string results in %d\n",result_y);
case a) string 2abc is 2 as decimal
case b) 2abc is NOT a simple positive integer!
there are either no digits or non-digits
case c) a null string results in 0
Perl is more generous and gives warnings if they are enabled. This will give a warning:
my $a = "23B";
You can check yourself if \D exists in string or if null string. Otherwise Perl will do the "best that it can".
Also of note is that in Perl,
everything is a string a string that looks like a number is still a string until used in a numeric
context. Consider this:
my $x = "00012";
This is a "trick" that I sometimes use to delete leading zeroes.