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).

164 questions
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
  • 56,245
  • 27
  • 137
  • 181
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
  • 5
  • 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,383
  • 2
  • 14
  • 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,040
  • 1
  • 19
  • 32
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
  • 477
  • 4
  • 7
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
  • 133
  • 7
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
  • 16,175
  • 3
  • 41
  • 59
3
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,473
  • 12
  • 14
3
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
  • 131
  • 1
  • 4
  • 14
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
  • 331
  • 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…
2
votes
4 answers

error C3861: 'strtoll': identifier not found

The main problem I am facing here is that strtoll() is flagged as an error in VC 2010 (error C3861: 'strtoll': identifier not found). Will it do the same thing if I replace it with strtol()? unsigned int get_uintval_from_arg(int argc, int index,…
John
  • 762
  • 2
  • 16
  • 33
2
votes
4 answers

How can I manually parse a custom DateTime format with optional fields in C#

Let's say I have the following dates/times in some in-house almost-ISO-like format: "2011-11-07T11:17" "--T11:17" (11:17 am, no date, only time) "-11-07" (november the 7th, no year, no time) The separators are mandatory, and enable me to know is a…
paercebal
  • 75,594
  • 37
  • 124
  • 157
2
votes
1 answer

strtoll not setting errno to ERANGE upon overflow

I'm parsing a long long value using fgets and strtoll as one does, but strtoll is not setting errno to ERANGE when an overflow occurs like it's supposed to. From the man page: The strtol() function returns the result of the conversion, unless the…
anastaciu
  • 20,013
  • 7
  • 23
  • 43
2
votes
2 answers

Segmentation Fault for numeric input

I'm writing my first ever program in C and it's giving me a lot of problems. It's fairly simple; input a number and the output will be the corresponding term in the Fibonacci sequence where the first and second terms are 1. It was initially working…
CybeatB
  • 57
  • 2
  • 9
1
2
3
10 11