C Programming – Understanding atoi and itoa functions

Converting one type of data to another type is the key thing when we deal with data using programming languages. C language supports this data conversion, and provides functions to ease our programming in C. Through this article we are going to discuss about atoi and itoa data conversion functions.

atoi function

This function is used to convert a string to an integer. Here is the syntax;

int atoi (const char *str);

It takes a string as an argument, converts the string into an integer and returns it. Returns a value 0, in case we pass an in-appropriate string. That means, if we pass a string with non-numeric characters, it returns the value 0. The return value is depending on the string we pass. Below table shows some examples;

String Return value
“Hello!” 0
“1234” 1234
“O2” 0
” 74″ 74
“3M” 3
“+768” 768
“-89” -89
“.23” 0

Observe that, when we pass a numeric string, it returns it’s numeric value. When we pass string starts with numeric characters along with non-numeric characters; the atoi function returns the numeric portion of it and simply ignores rest of the characters.

itoa function

Before start looking into this function; would like to mention, this is a non-standard function. Non standard functions may or may not be available in the version of C what you are using. During the compilation, the compiler will through an Error, if it fails to recognize these functions.

This function takes a numeric value as an argument and converts it into a string. The syntax of the function looks like below;

char *itoa (int value, char *buffer, int radix);

It takes a numeric value as an argument, and a buffer to store the converted string value. The conversion will happen based on the provided base system through it’s radix argument (usual values are 2, 8, 10 or 16).

Once it converts the string to an integer, it place the string into the buffer and returns it’s address. Then buffer address & return address are same, right? Yes. As I mentioned before, this is a non-standard C function; the implementation vary depending on the version of C you are using. Some providers, implement it as to return a NULL value when we pass wrong data through it. This is implementation specific, and non-standard.

Depending on the value you pass in radix, it converts the value into a string. If you pass 2, it converts the value into a binary string; if you pass 8, it converts the value into an octal string; etc.,

Here are some examples;

Value Radix Return String
20 10 “20”
20 2 “10100”
20 8 “24”
20 16 “14”
-123 10 “-123”
-20 2 “11111111111111111111111111101100”
0.2 10 “0”

Working Example

Here is the code to demonstrate what we discussed so far;

#include <stdio.h>
#include <stdlib.h> // need to include this header for atoi() and itoa() functions

void main()
{
	// keep enough memory to avoid issues
	char buf[256] = "\0";

	printf("%d\n", atoi("1234"));
	printf("%s\n", itoa(1234, buf, 10));
}

// Malin

Add a Comment

Your email address will not be published.