How to use atoi() and how to make own atoi() ?

atoi implementation in c

We use atoi to convert a numeric string to his integer value. atoi is a c library function and it takes a string as an argument and returns its integer value.In this article, we will see the atoi implementation in c and its important features.

Syntax:

A simple program to describe the functionality of atoi()

OutPut: 1234

Some Important Points of atoi()

  • If an alphabetic character comes in between of a numeric string then conversion only perform till the alphabetic character.

In below example ‘a’ occur between the string. So atoi() convert only till ‘a’.

OutPut:  12

  • If an alphanumeric character comes at the beginning of string then atoi() return 0.

In below example, an alphabetic character ‘a’ come at the beginning of the string.We already know that if alphabetic characters occur in between of the string then atoi()function immediately stop the conversion.So in this example atoi() return 0

OutPut: 0

  • If the length of numeric string greater than the range of integer then you couldn’t get a real value.

OutPut: 1215752192

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

Program to implement own atoi() in C

Source Code 1

OutPut: 1230

Source Code 2

OutPut: 1230

Source Code 3

The above methods do not handle the negative number and invalid pointer, so below another simple method that handles negative number and the invalid pointer.

OutPut: -1230



2 comments

Leave a Reply