Questions tagged [strtol]

strtol() is the C runtime library function for converting the text representation of a number to a long integer. This SO tag also applies to strtoll(), strtoul(), and strtoull() which perform the same conversion to types "long long", "unsigned long", and "unsigned long long".

Use this tag on for all questions about the use of the strtol() family of functions or where such a function does not seem to be working correctly.

Closely related are:

  • for converting text to int (includes atol(), atoll(), and atoq())
  • atol() for long
  • for double
  • for double with unambiguous error indication
  • for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

              long int strtol   (const char *nptr, char **endptr, int base);
         long long int strtoll  (const char *nptr, char **endptr, int base);
     unsigned long int strtoul  (const char *nptr, char **endptr, int base);
unsigned long long int strtoull (const char *nptr, char **endptr, int base);

BSD also has

                quad_t strtoq   (const char *nptr, char **endptr, int base);
              u_quad_t strtouq  (const char *nptr, char **endptr, int base);

strtol() converts from text in any base from 2 through 36. Or it can choose a base automatically (if base is specified as zero) same as the C compiler does depending on how the number is written: a leading 0 chooses octal, a leading 0x or 0X chooses hexadecimal, else decimal. Leading whitespace is skipped.

If endptr is not NULL, the pointer is set to the next character not converted. A conversion error is indicated by errno set to EINVAL (but set errno to zero before calling).

strtol() returns the result of the conversion, unless the value would underflow or overflow. It returns LONG_MIN if an underflow occurs, and LONG_MAX in case of an overflow. In both cases, errno is set to ERANGE. Similar is the case for strtoll() (LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX respectively).

175 questions
4
votes
3 answers

string to integer conversion edgecase handling

I want to convert a number, given in string format to an integer. I've found multiple solutions for this, like atoi() or strtol(). However, if not given an Input, that could be converted to an integer, like strtol("junk", &endptr, 10) I just get…
dedpunkrz
  • 69
  • 4
4
votes
0 answers

Proper end pointer with strtol() and "0x"?

strtol("0x", &endptr, 16); This completes with endptr pointing to "0x". I expected "x". Is my C library's strtol() amiss, my expectations or something else? The "0x" appears to be the beginning of an optional "0x" prefix, yet since it is not…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
4
votes
5 answers

Do I need to cast the result of strtol to int?

The following code does not give a warning with g++ 4.1.1 and -Wall. int octalStrToInt(const std::string& s) { return strtol(s.c_str(), 0, 8); } I was expecting a warning because strtol returns a long int but my function is only returning a…
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
4
votes
4 answers

Convert a string to int (But only if really is an int)

In college I was asked if our program detects if the string enter from command line arguments is a integer which it did not(./Program 3.7). Now I am wondering how I can detect this. So input as for example a is invalid which atoi detects, but input…
Alfred
  • 60,935
  • 33
  • 147
  • 186
4
votes
2 answers

Is there a C function to get permissions of a file?

I am writing a c program to be run on UNIX, and attempting to utilize the chmod command. After consulting the man pages, i know that chmod needs two parameters. first is the permission bits, second is the file to be changed. I want to take the…
trawww
  • 103
  • 1
  • 6
  • 14
4
votes
1 answer

Why does strtol require a pointer to a pointer rather than a single pointer?

Here's the prototype for the C standard library routine strtol: long int strtol(const char* str, char** endptr, int base); Typical usage: const char* str = "123a"; char* endptr; long int value = strtol(str, &endptr, 10); if (*endptr) // Do…
ffhaddad
  • 1,653
  • 13
  • 16
4
votes
3 answers

Using strtol to validate integer input in ANSI C

I am new to programming and to C in general and am currently studying it at university. This is for an assignment so I would like to avoid direct answers but are more after tips or hints/pushes in the right direction. I am trying to use strtol to…
Lukaaaaaaaay
  • 141
  • 1
  • 4
  • 14
4
votes
1 answer

Converting string to long using strtol and pointers

My goal is to convert a string such as "A1234" to a long with value 1234. My first step was to just convert "1234" to a long, and that works as expected: #include #include int main(int argc, char **argv) { char* test =…
user2461391
  • 1,433
  • 3
  • 16
  • 26
4
votes
1 answer

weird crash with strtol() in C

I was making some proves with strtol() from stdlib library because i had a program that always crashed and i found that this worked perfectly: main(){ char linea[]="0x123456",**ap; int num; num=strtol(linea,ap,0); printf("%d\n%s",num,*ap); } But…
Mark E
  • 3,403
  • 2
  • 22
  • 36
3
votes
1 answer

Why does function `strtoll` gives a wrong value and set errno to 34?

Here's my C code: char *ptr = "0xfff1342809062Cac"; char *pEnd; long long value = strtoll(ptr, &pEnd, 0); printf("%lld\n", value); printf("errno: %d\n", errno); I compiled it with gcc-8.3.0, and the output…
Sean
  • 1,055
  • 11
  • 10
3
votes
6 answers

using strtol on a string literal causing segmentation fault

I have a string that I get by getline() (more precisely I use a loop and getline to read a file line by line) Let's say the line is 12|34| Then I use strtok() to cut it down by substr = strtok(line, "|"); and store them into a string array with a…
Kent Wong
  • 143
  • 8
3
votes
1 answer

Testing `errno` after calling `strtol` returns "No such process"

Even though the string conversion succeeds, testing errnoreturns a value indicating an error: #include #include const char* numberString = "7"; char* endPtr; errno = 0; long number = strtol(numberString, &endPtr,…
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
3
votes
1 answer

Error handling after doing strtol

I am trying to read several numbers on stdin, one number on each line. I want to ignore any trailing text after number and strings if any on any line. To implement this I used the below code: while (getline(cin, str)) { num = strtol(str.c_str(),…
neham
  • 341
  • 5
  • 18
3
votes
1 answer

STM32 atoi and strtol sometimes missing first 2 digits

I am reading a value sent over RS485 which is the value of an encoder I first check if it has returned an E character (the encoder is reporting an error) and if not then do the following *position = atoi( buffer ); // Also tried *position…
3
votes
2 answers

Convert hexadecimal string to long

I am trying to do an hexadecimal to integer conversion on a 32 bit machine. Here is the code I am testing with, int main(int argc,char **argv) { char *hexstring = "0xffff1234"; long int n; fprintf(stdout, "Conversion results of string:…
jpmuc
  • 1,092
  • 14
  • 30
1
2
3
11 12