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

c atoi() function not working

Here is my code. The statement, int value=atoi(ptr[index]) in findMin() function ,gives an error as shown in screenshot. The strange thing is that when I use the same atoi() function in main, everything works pretty fine but it screams in…
Aisha Javed
  • 169
  • 13
-3
votes
1 answer

Return of atoi() - C function

I have random strings and i want to sort them. I need to find those containing only digits (like xyz..., x,y,z are digits); What function to use? I have tried atoi("3=fyth433"). But that returns 3. I was expecting it to return 0 for a string that…
Bill N.
  • 15
  • 2
-3
votes
2 answers

Printing out extra numbers? (C)

Hello I'm writing a program right now that read in values from files. It reads in the values as strings by fgets and then I wanted to change one of the strings into integers: #include #include #include #include…
Joe
  • 11
  • 4
-3
votes
5 answers

c++ atoi crashes at 1 million and above

I have searched for a couple hours now and I can't seem to find anything relevant to this problem. If you have found this problem elsewhere please point me to where you found it. I am using the atoi(const char *) function in c++ and all is working…
BradStell
  • 371
  • 3
  • 17
-3
votes
2 answers

if(str == NULL || str.length() == 0) get error

Here is the code for a leetcode algorithm problem: class Solution { public: int myAtoi(string str) { if(str == NULL || str.length() == 0) return 0; int pos = true; int result = 0; int i = 0; …
user2916610
  • 765
  • 1
  • 5
  • 12
-3
votes
3 answers

API to convert character to a string in C

Is there an API to convert a character to a string in C? I basically have a string and want to insert a character at the end of it. Say, char str[] = "hello"; I want it to look like "hello1"; I am looking for a convenient API instead of something…
user121
  • 305
  • 1
  • 3
  • 9
-3
votes
3 answers

Saving integers in vector from string

I have this string which contains numbers divided from one another with a /, like this 24/2/13. I want to save them individually in a vector of int,but it gives me this mistake expected unqualified-id before '.' token|. I know it might be a stupid…
-3
votes
4 answers

Using atoi to check string

i am trying to use atoi to convert a string into an int, and then check if the string was indeed all integers. Here is my code: printf("Please input the port: "); scanf("%s",port_number); int i = atoi(port_number); if(i != port_number) { errno =…
anderish
  • 1,709
  • 6
  • 25
  • 58
-3
votes
4 answers

Using with atoi() in C

I'm trying to use atoi() but I need the 0 in the front, but the function ignores it sprintf(str, "%d%d%d%d",comp[cont][0],comp[cont][1],comp[cont][2],comp[cont][3]); conv=atoi(str); printf("%d \n",conv); When I print str: 0100 And conv:…
-3
votes
3 answers

Parse human-readable sizes (k, M, G, T) into bytes in C

I'm looking for a quick way to parse human-readable byte sizes (examples: 100, 1k, 2M, 4G) into a byte values. The input is a char * and the output must be a size_t (e.g. unsigned, likely 64-bit or 32-bit integer, depending on architecture). The…
ɲeuroburɳ
  • 6,990
  • 3
  • 24
  • 22
-3
votes
1 answer

How Does C++'s atoi work?

So I have the following code: void Start(int &year, string &mon, char &nyd) { printf("%s", mon); int month= atoi(mon.c_str()); printf("%i", month); } When the incoming parameter is "03" (the first printf shows 03), I got 0 for…
user1447343
  • 1,417
  • 4
  • 18
  • 24
-4
votes
1 answer

How to use atoi with an int and malloc?

When I try and use atoi with an int and malloc I get a bunch of errors and key is given the wrong value, what am I doing wrong? #include #include #include struct arguments { int key; }; void argument_handler(int…
aLongBoat
  • 59
  • 9
-4
votes
1 answer

Why don’t the results of atoi() appear in my program’s output?

I’m trying to convert arguments passed on the command line to int using atoi, but it is taking forever no matter whether the string is small or big. Any ideas? int main(int argc, char *argv[]) { int id; int…
user3043108
  • 965
  • 1
  • 8
  • 10
-4
votes
2 answers

Write int to binary file in C

I have to open a file, which contains every number in new line. ( represented as string ) To read data I need to use getline(). To change data from string to int, I need to use atoi(). To write changed data to file, I need to use write(). What I…
yack
  • 106
  • 2
  • 12
-4
votes
2 answers

atoi() - from char to int

char c; int array[10][10]; while( !plik.eof()) { getline( plik, text ); int string_l=text.length(); character_controler=false; for(int i=0; i
MSB MSB
  • 13
  • 1
  • 5
1 2 3
28
29