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
334
votes
13 answers

How to convert a string to integer in C?

I am trying to find out if there is an alternative way of converting string to integer in C. I regularly pattern the following in my code. char s[] = "45"; int num = atoi(s); So, is there a better way or another way?
user618677
  • 4,909
  • 6
  • 23
  • 24
119
votes
2 answers

Where did the name `atoi` come from?

In the C language where did they come up with the name atoi for converting a string to an integer? The only thing I can think of is Array To Integer for an acronym but that doesn't really make sense.
Earlz
  • 62,085
  • 98
  • 303
  • 499
68
votes
6 answers

What is the difference between sscanf or atoi to convert a string to an integer?

gcc 4.4.4 c89 What is better to convert a string to an integer value. I have tried 2 different methods atoi and sscanf. Both work as expected. char digits[3] = "34"; int device_num = 0; if(sscanf(digits, "%d", &device_num) == EOF) { …
ant2009
  • 27,094
  • 154
  • 411
  • 609
59
votes
1 answer

What is the difference between std::atoi() and std::stoi?

What is the difference between atoi and stoi? I know, std::string my_string = "123456789"; In order to convert that string to an integer, you’d have to do the following: const char* my_c_string = my_string.c_str(); int my_integer =…
Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
58
votes
3 answers

atoi — how to identify the difference between zero and error?

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ Return Value On success, the function returns the converted integral number as an int value. If no valid conversion could be performed, a zero value is returned. If the correct value is out…
Nahum
  • 6,959
  • 12
  • 48
  • 69
43
votes
1 answer

Getting warning in C for 'atoi' function

I'm currently coding for a challenge question in a book I'm reading. My code executes perfectly with the correct output, but i"m getting a warning in my code and I'm just wondering why. I'm getting a warning on the line that reads: int…
user3249
  • 433
  • 1
  • 4
  • 5
41
votes
6 answers

How do I tell if the c function atoi failed or if it was a string of zeros?

When using the function atoi (or strtol or similar functions for that matter), how can you tell if the integer conversion failed or if the C-string that was being converted was a 0? For what I'm doing, 0 is an acceptable value and the C-string being…
Jared
  • 1,254
  • 7
  • 19
  • 26
39
votes
1 answer

atoi() from hex representation string

Need to make int from hex representation string like "0xFA" or better "FA". Need something like atoi("FA"). Is there are any standard solutions for that?
vico
  • 17,051
  • 45
  • 159
  • 315
35
votes
7 answers

What is atoi equivalent for 64bit integer(uint64_t) in C that works on both Unix and Windows?

I'm trying to convert 64bit integer string to integer, but I don't know which one to use.
JosephH
  • 8,465
  • 4
  • 34
  • 62
33
votes
3 answers

Safely convert std::string_view to int (like stoi or atoi)

Is there a safe standard way to convert std::string_view to int? Since C++11 std::string lets us use stoi to convert to int: std::string str = "12345"; int i1 = stoi(str); // Works, have i1 = 12345 int i2 = stoi(str.substr(1,2));…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
30
votes
2 answers

How to implement atoi using SIMD?

I'd like to try writing an atoi implementation using SIMD instructions, to be included in RapidJSON (a C++ JSON reader/writer library). It currently has some SSE2 and SSE4.2 optimizations in other places. If it's a speed gain, multiple atoi results…
the_drow
  • 18,571
  • 25
  • 126
  • 193
29
votes
5 answers

equivalent of atoi

Is there a function that could replace atoi in c++. I made some research and didn't find anything to replace it, the only solutions would be using cstdlib or implementing it myself
Mansuro
  • 4,558
  • 4
  • 36
  • 76
29
votes
2 answers

Equivalent of atoi for unsigned integers

I'm doing two operations involving atoi and I'm wondering how I can do this with unsigned integers because atoi seems to convert these to signed causing a wraparound integer overflow. I want to work with 32bit unsigned integers but atoi is limiting…
John
  • 341
  • 1
  • 6
  • 13
28
votes
2 answers

strconv.Atoi() throwing error when given a string

When trying to use strconv on a variable passed via URL(GET variable named times), GoLang fails on compilation stating the following: multiple-value strconv.Atoi() in a single-value context However, when I do reflect.TypeOf I get string as the…
dead beef
  • 673
  • 2
  • 6
  • 20
27
votes
6 answers

atoi implementation in C

I can't understand the following atoi implementation code, specifically this line: k = (k << 3) + (k << 1) + (*p) - '0'; Here is the code: int my_atoi(char *p) { int k = 0; while (*p) { k = (k << 3) + (k << 1) + (*p) - '0'; …
Adam
  • 1,944
  • 3
  • 17
  • 19
1
2 3
28 29