Questions tagged [atoi]

atoi() is the C runtime library function for converting the ASCII representation of a number to an integer. This SO tag also applies to atol(), atoll(), and atoq() which perform the same conversion to types "long" and "long long".

Use this tag on for all questions about the use of the atoi() family of functions or where it does not seem to be working correctly.

Closely related are:

  • for double
  • for converting to long from text in any base from 2 through 36, with unambiguous error checking. Or choose a base automatically depending on how the number is written. strtoul() converts to an unsigned long.
  • for double
  • for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

       int atoi (const char *nptr);
      long atol (const char *nptr);
long long atoll (const char *nptr);
 long long atoq (const char *nptr);

BSD-based libraries may declare the last as

    quad_t atoq (const char *nptr);

These functions all return zero if there is any problem during the conversion. Since converting a zero returns the value of zero, there is no easy way to distinguish an error from a correct conversion of zero. Use the scanf() or strtol() functions if such a check is needed.

Leading whitespace is skipped. An optional leading sign (+ or -) is accepted. The scan stops at the first non integer digit.

433 questions
20
votes
2 answers

Binary String to Integer with 'atoi()'

I have a string of binary that I then convert to an integer using atoi(). When I do this it seems to automatically convert the binary to decimal. The issue is that the resulting integer is negative and doesn't agree with any of the online…
SkippyNBS
  • 687
  • 1
  • 5
  • 21
20
votes
4 answers

Convert std::string to integer

I'm trying to convert a std::string stored in a std::vector to an integer and pass it to a function as a parameter. This is a simplified version of my code: vector record; functiontest(atoi(record[i].c_str)); My error is as…
Daniel Del Core
  • 3,071
  • 13
  • 38
  • 52
18
votes
11 answers

like atoi but to float

Is there a function similar to atoi which converts a string to float instead of to integer?
lital maatuk
  • 5,921
  • 20
  • 57
  • 79
17
votes
1 answer

What do atoi, atol, and stoi stand for?

I understand what said functions do, but I can't guess how their names were created, except that the last letter is from the return type.
yesennes
  • 1,147
  • 1
  • 10
  • 19
14
votes
4 answers

C++ - error while using atoi

I am trying to use the atoi function in order to obtain conversion from string to int. The thing is that I have a string array which contains both integers and string values. From what I've read, in order to get the error code from it, the function…
Simon
  • 4,999
  • 21
  • 69
  • 97
12
votes
9 answers

How to check to ensure you have an integer before calling atoi()?

I wish to take an integer as a command line argument, but if the user passes a non-integer string, this will cause a stack overflow. What is the standard way to ensure atoi() will be successful?
cilk
  • 231
  • 1
  • 2
  • 5
10
votes
1 answer

implicit declaration of function ‘atoi’?

Why am I getting an error when using the atoi() function? #include #include int main() { char s1[10], s2[10]; int x=5, y=6, z; sprintf(s1, "%d", x); sprintf(s2, "%d", y); strcat(s1, s2); z = atoi(s1); …
Chandra Sekhar Bala
  • 190
  • 1
  • 1
  • 12
10
votes
4 answers

Why do I get this unexpected result using atoi() in C?

I don't understand the results of the following C code. main() { char s[] = "AAA"; advanceString(s); } void advanceString(p[3]) { int val = atoi(p); printf("The atoi val is %d\n",val); } Here the atoi value is shown as 0, but I…
maddy
  • 827
  • 2
  • 11
  • 19
9
votes
6 answers

Using atoi with char

Is there a way of converting a char into a string in C? I'm trying to do so like this: char *array; array[0] = '1'; int x = atoi(array); printf("%d",x);
John Doe
  • 165
  • 2
  • 2
  • 4
8
votes
6 answers

Understanding an atoi() function

I'm a python programmer getting to learn C from the K&R book. This will seem like an awfully trivial question, but I'm stumped nonetheless. Attached below is a snippet of code from the K&R (RIP Ritchie!) book which implements the atoi()…
Craig
  • 1,929
  • 5
  • 30
  • 51
8
votes
1 answer

the base argument of std::stoi

The stoi function of c++ is defined as: int stoi(const std::string& str, std::size_t* pos = 0, int base = 10); as you can see, the base argument is defaulted as 10, so by default it could only handle decimal numbers. By setting base to 0, it could…
fluter
  • 13,238
  • 8
  • 62
  • 100
8
votes
7 answers

C programming: print only int from fgets

See this main: int main(void) { int i; int ch; char str[512]; fgets(str, sizeof str, stdin); for (i = 0; i <= (strlen(str)); i++) { if (str[i] != '\0' && str[i] != '\n') { int num =…
user2908206
  • 265
  • 3
  • 16
8
votes
1 answer

What can I assume about the behaviour of atoi() on error?

The standard C library function atoi is documented in ISO 9899:2011 as: 7.22.1 Numeric conversion functions 1 The functions atof, atoi, atol, and atoll need not affect the value of the integer expression errno on an error. If the value of the…
fuz
  • 88,405
  • 25
  • 200
  • 352
8
votes
2 answers

NASM Assembly convert input to integer?

Ok, so I'm fairly new to assembly, infact, I'm very new to assembly. I wrote a piece of code which is simply meant to take numerical input from the user, multiply it by 10, and have the result expressed to the user via the programs exit status (by…
user2862492
  • 101
  • 1
  • 1
  • 2
8
votes
8 answers

Understanding how to create atoi; How are characters compared?

I am trying to improve my understanding of C++, pointer arithmetic especially. I use atoi pretty often, but I have rarely given thought as to how it works. Looking up how it is done, I understand it mostly, but there is one thing that I am confused…
Joshua
  • 4,270
  • 10
  • 42
  • 62
1
2
3
28 29