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
0
votes
2 answers

Getting gibberish instead of numbers using memcpy and strtoul

I have the following piece of code compiling under gcc: int parseMsg(const char *msg_to_parse, unsigned long *exp_input, unsigned long *sysTicks ) { int l_msg_size = strlen(msg_to_parse); if(l_msg_size <10) return -1; char…
b3bel
  • 375
  • 1
  • 5
  • 15
0
votes
3 answers

End condition for a factorising loop

Good evening, I'm having trouble with an assignement. Basically we're required to code a program which will calculate the prime factors of a given stdin. The data may only enter the program through its stdin, be it throuh an echo or a < file.txt.…
JSG_ESP
  • 45
  • 1
  • 7
-1
votes
2 answers

How do I convert a number from the command line to an inter using strtol in C?

I started a course in C today and I am not really sure how you would use the strtol function to convert a string of an integer from command line arguments instead of using the atoi function (for some reason, I am not allowed to use the atoi…
neRienn
  • 41
  • 3
-1
votes
1 answer

Wrong atoi() output in some parts of an array

I'm reading an input file and writing it to a 2D integer array. All the numbers in the file have two characters. It works good but in all the [48]–[57] elements the integers have a redundant digit (e.g., 30, 871, 447 instead of 3, 87, 44). The same…
-1
votes
2 answers

About what is leftover after running strtol

I'm having trouble understanding the behavior of strtol() in C. For example, when I'm testing the string " 4396", if I have: char *test = " 4396"; char *ptr; int result = strtol(test, &ptr, 10); printf("%d\n", result); printf("%d\n",…
Ioce
  • 19
  • 4
-1
votes
2 answers

Why strtol() returns -1 for very large numbers?

Why strtol() returns -1 for very large numbers in C? For example: #include #include int main() { long long i = strtoll("135898539853985649864867468746876587784760", NULL, 10); printf("i = %lld\n", i); return 0; }
molexi
  • 530
  • 5
  • 13
-1
votes
1 answer

strtoul giving unexpected output

I am trying to use the strtoul function, but as shown below it is returning an unexpected value (adding ff's in the beginning): #include #include #include main() { unsigned long temp ; char *err; temp =…
mezda
  • 3,537
  • 6
  • 30
  • 37
-1
votes
1 answer

strtol result mismatch while string conversion

After checking out this question I did not found required solution, so I've tried to use strtol in following manner: in = (unsigned char *)malloc(16); for(size_t i = 0; i < (size_t)strlen(argv[2]) / 2; i+=2 ) { long tmp = 0; tmp =…
im_infamous
  • 327
  • 1
  • 3
  • 17
-1
votes
2 answers

Convert string to int in c

What I am trying to do is to access a string value in a multidimensional string array in C. The string is actually a number value which I want to store in an integer value. When I try to print the value as following printf("TESTING COMMAND\n"); …
Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61
-1
votes
3 answers

null in atoi converts the value to 0

In my program, I have a char array (char data[]) from which i have to extract the data and store it in an integer variable(value). To do so, I have used atoi in my program. Problem is that I sometime receive null in the char array(data). If i use…
Kung-fu-panda
  • 47
  • 1
  • 1
  • 8
-1
votes
2 answers

linux C string to int

I'm trying to convert this string "09195462965" to an int but I'm running into problems. snippet of my code: int n, p, pnum=0; char buffer[256]; char *endptr; long pnumber; bzero(buffer,256); p = read(sock,buffer,255); pnumber = strtol(buffer,…
user1553142
  • 237
  • 1
  • 11
  • 21
-2
votes
3 answers

Using strtoul in C

In C, why does strtoul(argv[1]) just doesn't work? It looks like more parameters are needed but I can't prevent how long the number will be. Thanks! p.s. (argv[1] is properly setted).
Joseph
  • 401
  • 2
  • 6
  • 10
-2
votes
1 answer

Strtol doesn't return the correct endptr - C

I have to parse data from file rows and for that I'm now using strtol() function. I have for example this row in the text file: 1 abc This is for example, an invalid row as this specific row has to contain one and only one integer value. Now when…
HelpMe
  • 91
  • 10
-2
votes
1 answer

Casting Long to Unsigned Short Int / Strtol

There is no compile error. However, the results are not expected. My suspicion is that the casting long to unsigned short int is causing the error. How can I safely cast long to a short for Hex? [Requiriments: Convert a row of string (which has…
user9135060
-2
votes
1 answer

strtol not working as expected

I am unable to convert a string to a long using strtol. Having a leading "." before the number in the string returns 0. Without the "." strtol returns 3456 as expected. #include
homeGrown
  • 375
  • 1
  • 8
  • 25
1 2 3
11
12