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
-4
votes
3 answers

Printing atoi value of an uninitialised array

Simple question, below is my code. The number my compiler returns is 4219296. My question is: why? What is my code doing here? #include char array[]; int main() { atoi(array); printf("%d \n", array); return 0; }
daviegravee
  • 171
  • 2
  • 12
-4
votes
1 answer

How does atoi() actually work

This is the Code : #include /* atoi: convert s to integer; version 2 */ int atoi(char s[]) { int i, n, sign; for (i = 0; isspace(s[i]); i++) /* skip white space */ ; sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') …
HELP PLZ
  • 41
  • 1
  • 9
-4
votes
1 answer

atoi conversion segmentation fault working on windows but not in ubuntu

#include #include vector > vector2; vector vector; for(int i = 0; i < vector.size();i++) { vector2[atoi(vector[i+1].c_str())].push_back(vector[i]); i++; } I try this on Windows, its working…
user1745860
  • 207
  • 1
  • 5
  • 11
-4
votes
1 answer

char* array keeps being set to nullptr

Here is my code: int main () { const int MAX = 10; char *buffer = nullptr; // used to get input to initialize numEntries int ARRAY_SIZE = 20; // default is 20 entries int numEntries; success = false; …
Bbvarghe
  • 267
  • 2
  • 5
  • 18
-5
votes
1 answer

C - Check if given argument is natural number

How should I correctly check if given argument is a natural number in C? I am a very beginner when it comes to C... I already have compared that argument to 1 and 2 by atoi(argv[1]) == 1 ..., but when I pass let's say 1.2137 as an argument, atoi…
siwakw7
  • 17
  • 1
  • 6
-5
votes
1 answer

atoi to place part of string into array[0]

I'm taking in input from the user as a string. I want to transfer the input to an integer array. I'm using atoi, but it places the entire input from the user into each part of the integer array. How do I get this to happen: string input =…
Katy
  • 13
  • 2
-5
votes
2 answers

Undefined Behaviour of atoi function

The following code is showing a strange behaviour : int main() { char numArr[] = {'9','8','5'}; int num; printf("%d\n",num); num = atoi(numArr); printf("%d\n",num); return 0; } I haven't initialized the num so it contains a…
BigO
  • 98
  • 1
  • 12
-5
votes
4 answers

Converting a [char] variable to [int] then back to [char]

I am using Visual C 6 I am trying to convert a character array (single-quotation) into an integer, then incrementing the value by 1, then storing the result back into a different character array.. But I keep getting an unexpected value when…
Ahmad
  • 12,336
  • 6
  • 48
  • 88
-5
votes
2 answers

Program segfaults when I call the atoi() function

I am having segfaulting problems with my program that is supposed to read in a file containing bowling scores and then output your total score, it calls the atoi() function it segfaults. #include #include /* int myAtoi(char…
Mike Shlanta
  • 168
  • 1
  • 8
-6
votes
2 answers

C : how do I printf "the square root of 1764 is 42 and * in ascii"?

ok so I am learning C and I try to use simple functions to understand basics and here I am stuck whith a segmentation fault I can't manage to make this code working h3lp please thanks you all !!! #include #include int …
CnewB
  • 1
  • 3
-6
votes
1 answer

Do I need to include and for c_str(), atoi and atof functions?

I'm using c_str(), atoi and atof functions for converting string variables to integer or float/double. For example, val = atoi(val1.c_str()); val = atof(val1.c_str()); So, I would like to know if I need to include and . Thanks.
-7
votes
1 answer

How to avoiding nullchar converted to 0?

I have a char array that is filled with {'1','0','0','0'} for example, I apply atoi() on this array it returns 1000 which is true, however after I reset the array elements to null char '\0' and refill the array with 50 and apply atoi() again it…
-8
votes
2 answers

Is atoi multithread safe?

I am experiancing some error while creating a multithreaded program. While using gdb to debug, the atoi function is throwing error. Please help, is atoi multithread unsafe and if so, what are the alternatives?
Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27
1 2 3
28
29