[SOLVED] C programming linux - strtol does not set errorno on failure
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C programming linux - strtol does not set errorno on failure
According to the man page, strtol should set the errno:
Quote:
Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN (LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure,
the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a
non-zero value after the call.
However, it appears not to:
Code:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
main(argc, argv)
char **argv;
{
char *val = "turdburger";
int ival;
errno = 0;
ival = strtol(val,(char **)NULL, 10);
if ( errno )
{
printf("FAIL - (%d)(%s)\n",errno,strerror(errno));
}
else
{
printf("apparently it worked %s=%d \n",val,ival);
}
}
[root]# ./a.out
apparently it worked turdburger=0
[root]# uname -a
Linux xxx 2.6.22.14-72.fc6 #1 SMP Wed Nov 21 14:10:25 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
The strtol() function converts the initial part of the string in nptr
to a long integer value according to the given base, which must be
between 2 and 36 inclusive, or be the special value 0.
This means it only uses the digit chacacters at the left of the string. It stops at the first non-digit, and returns the number. It's zero for no digits at all. The only error it returns, is if the number is too big or too low for "long" variables.
This means it only uses the digit chacacters at the left of the string. It stops at the first non-digit, and returns the number. It's zero for no digits at all. The only error it returns, is if the number is too big or too low for "long" variables.
Is there anyway in C to check weather a string is indeed an integer value?
You mean to check if the string contains digits only? I don't think there is one, but it's easy to make one - looping thru the characters and check each with isdigit.
Another way:
Code:
if (strspn(s,"0123456789") == strlen(s)) {
/* s contains only digits */
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.