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
7
votes
6 answers

C - Comparing numeric strings

Out of professional curiosity, what is the safest / fastest / most efficient way to compare two fully numeric strings in C? #include #include #include int main(void){ char str1[5] = "123"; char str2[5] = "123"; char…
Valdogg21
  • 1,151
  • 4
  • 14
  • 24
7
votes
5 answers

How to sum all command line arguments in C?

I have an assignment. The program is to print the sum of all command line arguments in C. I tried this code it compiles but throws an error after passed arguments in the console. Below is the code. /* Printing sum of all command line arguments…
7
votes
2 answers

How do you use atoi to assign individual elements of a char array?

So as we all probably know, the atoi converts a char to a number. But, what do you do if you only want one of the array elements instead of the whole array? Please look at the following: for (h = 0; h < 5; h++) { num[h] =…
thyrgle
7
votes
2 answers

How do I fix a "no matching function for call to 'atoi'" error?

All indications tell me this is a ridiculously easy problem to solve, but I can't figure out error telling me the atoi function doesn't exist. C++ #include #include using namespace std; string line; int i; int main() { …
nipponese
  • 2,813
  • 6
  • 35
  • 51
6
votes
2 answers

c atoi() for wide chars on linux?

Is there a c atoi() equivalent for wide chars on Linux? I can find something for MS (wtoi) but I can find anything in a standard Linux lib.
harrije
  • 115
  • 1
  • 4
6
votes
3 answers

function opposite to atoi()?

I would like to convert an integer to a string like: int a = 12345 coverts to char b[6] = "12345" basically the opposite of the atoi function that converts a string to an integer.
Invisible Hippo
  • 124
  • 1
  • 1
  • 9
6
votes
2 answers

Forcing String to int Function to Consume Entire String

Given a string that should represent a number, I'd like to put it into a conversion function which would provide notification if the whole string did not convert. For input: "12": istringstream::operator>> outputs 12 atoi outputs 12 stoi outputs…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
7 answers

Non-Integer numbers in an String and using atoi

If there are non-number characters in a string and you call atoi [I'm assuming wtoi will do the same]. How will atoi treat the string? Lets say for an example I have the following strings: "20234543" "232B" "B" I'm sure that 1 will return the…
monksy
  • 14,156
  • 17
  • 75
  • 124
5
votes
3 answers

atoi function in C not working properly (when exceeding a certain value)

Might someone explain why the atoi function doesn't work for nmubers with more than 9 digits? For example: When I enter: 123456789, The program program returns: 123456789, However,when I enter: 12345678901 the program returns: -519403114... int main…
user997863
5
votes
1 answer

octal string to integer for open(O_CREATE) permissions

How to use an octal string from *argv[] for something like: open("outfile",O_CREAT | O_RDWR,0777); 0777 means permission in octal numbers. My code: int arC = atoi(argv[optind]); printf("argv optind %s after atoi %d\n",argv[optind],arC); int test…
5
votes
2 answers

int from string in go

What's the function to create a int value from string i := ???.????( "10" )
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
5
votes
1 answer

atoi() equivalent for intptr_t/uintptr_t

Is there a function in C++ (C++11, if it makes a difference) that converts a string to a uintptr_t or intptr_t? I can always use atoll() and cast it afterward, but it'd be nice to get a function that does 32-bit for 32-bit machines and 64-bit for…
atanamir
  • 4,833
  • 3
  • 24
  • 20
5
votes
4 answers

Using atoi to fill an array of ints

First time asking a question on here. Apologies if there's already threads about this but i had a few searches and didn't quite find what i think i was looking for. I'm very new to C and am working through a few homework exercises for my…
Chris Lyttle
  • 71
  • 2
  • 9
5
votes
3 answers

implement atoi() of C in PHP

There's a function in C, atoi(), implement this in PHP; $string = '5467'; //function should return 5467 as integer So this is what I found (its implementation in C) int myatoi(const char *string) { int i; i=0; while(*string) { …
vknyvz
  • 653
  • 7
  • 12
4
votes
5 answers

c++ weird problem converting a char to int

I'm a total newbie to C++ and I was trying to do one of the problems from Project Euler when I had a very weird problem. I reduced the error to the following. Consider the following simple code: #include using namespace std; int main()…
curial
  • 514
  • 4
  • 17
1 2
3
28 29