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
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
  • 794
  • 2
  • 18
  • 34
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
  • 81,378
  • 38
  • 130
  • 159
2
votes
5 answers

How can I convert hex encoded string to string in C efficiently

I need to convert hex encoded string like this: char hstr[9] = "61626364"; // characters abcd\0 Into "abcd" // characters as hex: 0x61 0x62 0x63 0x64 // hex "digits" a-f are always lowercase At this moment I wrote this function: #include…
Kamil
  • 13,363
  • 24
  • 88
  • 183
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
  • 23,467
  • 7
  • 28
  • 53
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
  • 3
  • 9
2
votes
2 answers

How to store a values with a min and max size in c

I'm trying to create a program which asks for a 13-16 digit credit card number, and re-prompts if the user enters non-numerical values. So far my program works when I enter 16 digit values; however, it re-prompts if I enter anything less. How do I…
Patrick Dankyi
  • 99
  • 1
  • 1
  • 7
2
votes
2 answers

Is it better to implement strtol() without errno?

Traditional strtol() is usually used like this: int main() { errno = 0; char *s = "12345678912345678900"; char *endptr; long i = strtol(s, &endptr, 10); if(i == LONG_MAX && errno == ERANGE) printf("overflow"); } We…
weiweishuo
  • 847
  • 7
  • 15
2
votes
2 answers

C tokenising using strtok is printing out unexpected values and is hindering my strtol validation

Trying to tokenise using strtok the input file is InputVector:0(0,3,4,2,40) Trying to get the numbers in but I encountered something unexpected that I don't understand, my tokenising code looks like this. #define INV_DELIM1 ":" #define…
alexW
  • 183
  • 3
  • 13
2
votes
4 answers

Casting hex string to signed int results in different values in different platforms

I am dealing with an edge case in a program that I want to be multi-platform. Here is the extract of the problem: #include #include void print_bits(size_t const size, void const * const ptr){ unsigned char *b = (unsigned…
Dumbo
  • 13,555
  • 54
  • 184
  • 288
2
votes
1 answer

C Parsing String Function Argument Using strtol

I'm a bit new to C and want to understand a few things about accessing function arguments using pointers and dereferencing. Here's my code, the whole point of the program is to use strtol to parse a given parameter with only two digits separated by…
HoopsMcCann
  • 347
  • 2
  • 3
  • 18
2
votes
3 answers

Floating point equivalent to strtol() in C

strtol converts the inputed string str to a long value of any specified base of 2 to 36. strtof() offers a similar functionality but without allowing your to specify base. Is there another function that does the same as strtof but allows you to…
2
votes
1 answer

Matrix-multiplication strtol

I am doing something wrong at strtoul function at aula0602.c file., and I can't print the three matrices. Apparently the files aula0601.h and aula0601.c are ok, but when I execute the binary file, none is printed at the command line. I write the…
2
votes
4 answers

strtol not changing errno

I'm working on a program that performs calculations given a char array that represents a time in the format HH:MM:SS. It has to parse the individual time units. Here's a cut down version of my code, just focusing on the hours: unsigned long…
Rez
  • 187
  • 12
2
votes
1 answer

sscanf() hex ints into an array of ints vs. unsigned chars

I am converting a string representation of a mac address into an array of UINT8s defined as unsigned char. I am curious why sscanf() will read all 0s when I read into an array of UINT8s and actual values when I read into an array of regular 32 bit…
ubtac
  • 33
  • 7
2
votes
5 answers

C's strtod() returns wrong values

I'm currently working on a banking terminal program for the laboratory exercises at my university. What puts me off my stride is a function supposed to take the user's input of amount to be transfered, check if it fits all requirements and if so,…
ryfterek
  • 669
  • 6
  • 17
1 2
3
11 12