atoi(), itoa() in C++

By: Manoj Kumar Emailed: 1643 times Printed: 2113 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

stdlib is something of a miscellaneous collection of functions that did not fit into the other libraries. It includes simple integer math functions, sorting functions (including qsort(), one of the fastest sorts available), and text conversions for moving from ASCII text to integers, long, float, and so forth.

The functions in stdlib you are likely to use most often include atoi(), itoa(), and the family of related functions. atoi() provides ASCII to integer conversion. atoi() takes a single argument: a pointer to a constant character string. It returns an integer (as you might expect). Listing below illustrates its use.

Using atoi() and related functions.

1:     #include <stdlib.h>
2:     #include <iostream.h>
3:
4:     int main()
5:     {
6:        char buffer[80];
7:        cout << "Enter a number: ";
8:        cin >> buffer;
9:
10:       int number;
11:       // number = buffer; compile error
12:       number = atoi(buffer);
13:       cout << "Here's the number: " << number << endl;
14:
15:       // int sum = buffer + 5;
16:       int sum = atoi(buffer) + 5;
17:       cout << "Here's sum: " << sum << endl;
18:     return 0;
19: }

Output: Enter a number: 9
Here's the number: 9
Here's sum: 14

Analysis: On line 6 of this simple program, an 80-character buffer is allocated, and on line 7 the user is prompted for a number. The input is taken as text and written into the buffer.
On line 10, an int variable, number, is declared, and on line 11 the program attempts to assign the contents of the buffer to the int variable. This generates a compile-time error and is commented out.

On line 12, the problem is solved by invoking the standard library function atoi(), passing in the buffer as the parameter. The return value, the integer value of the text string, is assigned to the integer variable number and printed on line 13.

On line 15, a new integer variable, sum, is declared, and an attempt is made to assign to it the result of adding the integer constant 5 to the buffer. This, too, fails and is solved by calling the standard function atoi().

 


NOTE: Some compilers implement standard conversion procedures (such as atoi()) using macros. You can usually use these functions without worrying about how they are implemented. Check your compiler's documentation for details.

C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

Amazing new function atoi() , it shows c,c++ are powerful languages than any other

Hari Prasad


View Tutorial          By: Hari at 2009-05-05 22:20:17
2. View Comment

i'm really hoping this example was wrote before 1998, you know, before c++ standard
iostream.h isn't in c++ standard, only iostream is, and after that using namespace std is missing or use std::cout...
And if you write pure c++ code try using cstdlib instead of stdlib.h and start using std::string instead of char[].
And example of converting string to integer:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
int number;
string stringNumber;
stringstream ss;
cin >> stringNumber;
ss << stringNumber;
ss >> number;
cout << "you have typed: " << number << endl;
return 0;
}


View Tutorial          By: Anonymous at 2009-06-07 21:00:18
3. View Comment

it's very nice answer

View Tutorial          By: ahmedmohsen at 2011-12-31 19:26:09

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Manoj Kumar
Importing Keystore example using keytool in Java
for loop example in Java
Result Sets, Cursors and Transactions in SQL
JSP Tags for SQL to connect to a database
Using the qsort() and bsearch() functions with values - example program in C
Using qsort() and bsearch() with strings - example program in C
scanf and sscanf sample program in C
Use of {0} is required and Accessing resource bundles programmatically in Struts
Character Arrays in C
Pointers to functions in C++
atoi(), itoa() in C++
Ticker sample program in J2ME
DateFormat sample program in Java
Random class sample program in Java - nextGaussian(), nextBoolean(), nextBytes(), nextInt(), nextLong(), nextFloat(), nextDouble()
What Is a Reference in C++?

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Using Comments in a C++ Program
Using cout.width() in C++
Dot (.) vs Arrow (->) to access data members in C++
Tutorial on Inline Implementation in C++
Calculate average using Two-Dimensional Array in C++
assert() example program in C++
Programming errors a compiler will detect in C++
Sorting an array of Strings in C++
Difference between Procedural, Structured, and Object-Oriented Programming
strcat() and strncat() sample program in C++
Compute the square root of the sum of the squares of an array in C++
Calculating total based on the given quantity and price in C++
C++ Destructors Versus Java Finalization
cin.ignore() in C++
Default arguments in C++
Most Emailed Articles (in C++)
Public versus Private members in C++
The indirection operator (*) - dereference operator.
const Pointers in C++
Returning values from a function in C++
while (1) Loops in C++
Sorting an array of Strings in C++
Using Comments in a C++ Program
Specifying default values to function parameters in C++
continue and break statements in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Calculating total based on the given quantity and price in C++
Demonstrating global and local variables in C++
while Loops in C++
Manipulating Data by Using Pointers