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
1 answer

Different output with printf

I am trying to print the value of a particularly large value after reading it from the console. When I am trying to print it from two different ways, one directly after assigning, one with the return value from the strtol function, I get different…
Coder
  • 1,415
  • 2
  • 23
  • 49
-2
votes
2 answers

C get numeric value from part of string

My task is to get long numeric value from part of char[], where I have the index of its begining and ending. I am just learning C, so it is confusing to me which function works the best in my case.. Lets say im making function getNum(): static…
hou
  • 3
  • 2
-2
votes
1 answer

strtol in c not giving me consistent values. How does strtol work?

I have written the following lexical analyzer. It works correctly for inputs like: c&3&f, (3|6)&c, f^1. However the results I am getting for strtol are not consistent. When I run <3|3 it converts hex 3 to decimal 8 for the first 3 value and then…
orelius
  • 13
  • 4
-3
votes
1 answer

Converting user input to an array of characters, and filtering letters from other characters?

#include "stdafx.h" #include "stdlib.h" #include int num = 0; int i = 0; int ch = 0; int letter_index_in_alphabet(int ch) { if (isalpha(ch) == true) { char temp_str[2] = { ch }; num = strtol(temp_str,…
Douggle07
  • 1
  • 1
-3
votes
1 answer

Best Way to Convert Array of Strings to Array of Int in C

My current problem is to read in an unknown number of integers from stdin. My approach is to use gets() to store the entire line as a char array (char str[50]). I am trying to parse the char array and convert each "string int" to an integer and…
Quinn Tai
  • 55
  • 1
  • 7
-3
votes
1 answer

C warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast

EXPLANATION: simply trying to convert a a char to hexadecimal but i keep getting this error and I'm not sure how to get around this PROBLEM: warning: passing argument 1 of ‘strtol’ makes pointer from integer without a cast[cs214111@cs lab3]$ vi…
cmehmen
  • 249
  • 1
  • 3
  • 12
-3
votes
2 answers

Very slow function

strtol is very slow for my program which I want it to be very fast so for example I have this string: PCHAR ex = "\x55\xEC" etc... They're hex, so If I want to write it without the "\x" to be like "55 EC" etc.. I have to use strtol, but strtol is…
Frought
  • 116
  • 9
-4
votes
2 answers

string with numbers change type to long int

I am writing in C++ and I have a string. I want to check if this string is only numbers and If it is I want to change the type to long int. stringT = "12836564128606764591"; bool temp = false; …
Christ
  • 3
  • 5
-5
votes
7 answers

Complex algorithm to extract numbers/number range from a string

I am working on a algorithm where I am trying the following output: Given values/Inputs: char *Var = "1-5,10,12,15-16,25-35,67,69,99-105"; int size = 29; Here "1-5" depicts a range value, i.e. it will be understood as "1,2,3,4,5" while the values…
Amrit
  • 2,295
  • 4
  • 25
  • 42
-9
votes
1 answer

Conversion char* string to hex uint16

I need to build a function, my input is a char *string and I'll need to get the "same representation" but in a uint16. For example: input: "12" --> output: 0x0012 input: "0" --> output: 0x0000 input "123" --> output: 0x0123 input "1234" --> …
menchopez
  • 65
  • 2
  • 7
1 2 3
11
12