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

atoi and strtol produce erroneous results

#include #include #include int main() { char *filename = "Sayilar.txt"; FILE *fp = fopen("D:\\WWW\\Yazilim\\cLionProject\\untitled3\\Sayilar.txt", "r"); if (fp == NULL) { printf("Error: could…
0
votes
1 answer

Clang-Tidy: 'fscanf' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead

Somewhat unexperienced with C here! I'm using CLion to write a program and keep getting this warning message whenever I use fscanf to store a value from an input file into a variable: Clang-Tidy: 'fscanf' used to convert a string to an integer…
0
votes
1 answer

C double printf() when a space is put in my scanf()

I've been trying to make this code work properly: #include #include int main(int argc, char const **argv) { char buf[100]; int var = 0; do { printf("Write a number :\n"); scanf("%s", buf); }…
0
votes
2 answers

After using fgetc(stdin) on a variable, how do I pass the input to strtol?

I have a variable where the user inputs one number. That variable is of type int because that's the return value of fgetc(stdin) and getchar(). In this case, I'm using fgetc(stdin). After the user inputs the number, I would like to convert it to an…
Matthew Schell
  • 599
  • 1
  • 6
  • 19
0
votes
4 answers

Why is the second argument not working with strtol?

I did this something like this: /* convert the argv[1] into Integer and store the result in key * using library function: strtol() to do it */ char **flag = NULL; key = strtol(argv[1], flag, 10); // if argv[1] is not all digits if (**flag !=…
Hiro
  • 110
  • 1
  • 7
0
votes
1 answer

Strtol function returning wrong value

I need to get a credit card number as input on an exercise, to dos so I'm ussing the function fgets (getting it as a string to check if it is realy a number) and then strtol to make it a long integer. But the integer value return by the function…
Luís Otávio
  • 183
  • 1
  • 13
0
votes
2 answers

why strtol returns zero?

im struggeling with a function. Goal is to check the user input (already sanitized) and act accordingly. the function looks like this: bool checkMove(board *Minesweeper,char *input){ char *tmp; switch(input[0]){ case '?': …
h1ghrise
  • 19
  • 4
0
votes
0 answers

Illegal Instruction 4 in C

I am writing a program to print out the atomic weight of the formula inputted. Everything works until I pass in "H2O2" or letter-number-letter-number. It displays the error illegal instruction and can't seem to find why it's not working. It reads…
Andy Lin
  • 3
  • 3
0
votes
2 answers

Using strtol to parse integral data and avoid invalid formats - C

So I want through the most topics related to using strtol in respect of inputs, but didn't find the exact issue I look for. Basically, I have to build some data structure in C (m-ary tree), and we receive the data of vertices from lines of the text…
HelpMe
  • 91
  • 10
0
votes
1 answer

How do I modify my code to use strtol() instead of scanf() to read in first two ints only?

My current code seems to work fine on DOS but not on Unix, and I need to make it work on both. From what I have found so far, it seems I should use strtol(). However, I cannot seem to figure out how to get strtol() to get only the first two…
0
votes
2 answers

reading an integer from a file

I have a code which suppose to read an integer from a file. But its actually reading as an character. Suggest me some modification where I can read the integers into an array. fptr =fopen("path","r"); while(1) { c=getc(fptr); putchar(c); if…
user685875
  • 5
  • 1
  • 4
0
votes
1 answer

How to fix 'No matching function for call to 'strtok''

I'm trying to use the function strtok to tokenize an input from the user and then print out the result with new lines between each word. However, this error pops up. #include #include #include using namespace…
Naoki Atkins
  • 9
  • 1
  • 3
0
votes
1 answer

Can't get strtol() to detect overflow

This program uses a lexical scanner to classify tokens as symbol, string, decimal number, hex number,… When a "number" is detected, it is handed over to strtol() to convert it to the internal 32-bit binary value. However I can't get strtol() to…
ajlittoz
  • 414
  • 1
  • 5
  • 14
0
votes
1 answer

The result of using strtol() under stdlib.h and stdio.h is different

When trying to parse a number too big to fit a long, strtol() returns 0 instead of LONG_MAX (stdio.h). If I read the POSIX spec correctly, it should be LONG_MAX. There is a different between stdio.h and stdlib.h #include "stdio.h" int main(void){ …
Zone233
  • 23
  • 1
  • 7
0
votes
3 answers

Converting a binary string to integer

When I try and convert my binary string to int I am receiving a couple of mistakes that I can not figure out. First I am reading from a file and the leading zeros are not showing up when I convert and the new line is showing zero. This code I am…
user5651011