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

Objective-c: Why is strtol method used in this code I inherited?

I recently inherited some Objective-C code and am really confused as to what it's actually doing? 1) I don't understand why the char byte_chars[3] is being populated by a '0' first and then a 0 at the end? 2) I don't understand why they used…
Kamilski81
  • 14,409
  • 33
  • 108
  • 161
0
votes
4 answers

Why does string to int conversion not print the number 0 if its at first index

I am wondering as to why when converting strings to int with either atoi or strtol it does not print the number 0 if its the first index, take this code for example char s[] = "0929784"; long temp = strtol(s, NULL, 10); printf("%li", temp); OUTPUT:…
Kevin H
  • 21
  • 3
0
votes
3 answers

C - Can't read user integer input

I'm trying to read user integer input with the following code. I'm aware that strtol returns a long value. long color = 1; char buf[256]; char * last; while(color != 0){ printf("Enter a color (0 to stop): "); fgets(buf, 256, stdin); …
user3308219
  • 157
  • 2
  • 11
0
votes
0 answers

Read from file, then strtok, then strtol, then memcpy to struct -stock which has unsigned variables

I am still new to C. I am reading from a file stock.dat: I0001|Meat Pie|Yummy Beef in Gravy surrounded by pastry|3.50|50 I0002|Apple Pie|Delicious Stewed Apple in a Yummy Pastry envelope|3.00|20 I0003|Lemon Cheesecake|A delicious, 1/8 size slice of…
0
votes
1 answer

What effect does locale have on strtol?

"The GNU C Library: Parsing of Integers" says, about strtol: In a locale other than the standard "C" locale, this function may recognize additional implementation-dependent syntax. Documentation on cppreference concurs: Additional numeric formats…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
0
votes
0 answers

strtod using classic format?

Now functions strtod and strtol support parsing hex number format, infinity, NaN and other non-standard formats. But how to convert string to number using only classic format: 123, 1.23, 0.123, -123, 1.23e10, 1.23e-10 ? Not: +123, .123, 0xabcd,…
iOS User
  • 101
  • 1
  • 8
0
votes
2 answers

Cant Split and Store hexstring in array

i wanted to split this hexstring, convert em, and store em in an array. but it seems there is something off from my work, and i don't know what. I intend to split this string 27CA6B to 27 CA 6B but the output is always only the first…
user6318361
  • 51
  • 1
  • 7
0
votes
0 answers

Vulnerable program using strtol

I came across this question when I was reading about vulnerable C programs involving strtol. Below is the code #include void f (int i, int j) { int a[50]; a[i] += j; } int main (int argc, char *argv[]) { int x = 10, y, z; …
0
votes
1 answer

Converting char array to int with strtol() in C

i have a difficulty with strtol() function in C, here's a piece of code of how i'm trying to use it char TempChar; char SerialBuffer[21]; char hexVoltage[2]; long intVoltage; do { Status = ReadFile(hComm,…
sentenced
  • 31
  • 2
0
votes
1 answer

Convert IP address from hex to dec using strtol()

I put an IP address from frame to struct: unsigned char destination_address[4]; In my main program I load a struct: struct ipv4 naglowek_ipv4; upakuj_ipv4(bufor_eth_ipv4, &naglowek_ipv4); And try show this in a "human-readable…
0
votes
1 answer

C programming, segmentation fault when assigning int member in struct to integer

I am programming in ansi C in a Linux environment, I am using gcc compiler. I have the following code #define USER_IN_LEN 15 #define EXTRA_SPACES 2 #define DELIMS " ,\n" //define the sruct typedef struct position { int x; int y; }…
Mohamad Mustafa
  • 65
  • 1
  • 2
  • 8
0
votes
1 answer

Can I use a custom "split character" in strtol

I've been reading some documentation about strtol here and in the example a guy uses space to show this funtion when a new number begins. I'm new to C++ and I don't know much abount pointers and how they work yet, so I decided yo ask you about it.…
Michael
  • 548
  • 6
  • 23
0
votes
1 answer

C Parse String Into Two Ints With Strtol

Assume I have a program that calculates the result of multiplying two integers together from a string. I use strtol to separate the first part but how do I separate the second int? For example, "12 5" would give a result of 60. Right now my code…
0
votes
2 answers

Why does strol need a radix when strtod does not?

I've been working with cin input recently, and I discovered that strtol needs a radix: long int strtol (const char* str, char** endptr, int base);, but strtod does not: double strtod (const char* str, char** endptr);. Obviously, double numbers can…
0
votes
3 answers

Properties of Scanf and strtol when printing out an integer representation of string

I know that strings are just an array of chars with adjacent memory addresses. So when you have a character array: char s[5]; s[0] = '1'; s[1] = '2'; s[2] = '3'; s[3] = '4'; s[4] = '5'; and change character array at s[1] to "5" then printing such…
john
  • 61
  • 8