C/C++ atoi() Function Tutorial – Convert String To Integer – POFTUT

C/C++ atoi() Function Tutorial – Convert String To Integer

C and C++ programming languages provide string or character to integer conversion with the atoi() function. atoi simply the short form of chArTOInteger where the uppercase letters stand side by side. The function is provided by the standard library which means we do not need to install extra library or package.

Syntax

atoi() function has a very simple syntax.

  • int integer type which is the type of returned value
  • const char * is a constant char array which is equal to a string which variable name is str

Include <stdlib.h> Header In C

atoi function is provided from the standard library which provides basic and popular functions for application development. So in order to use atoi() function stdlib.h the header should be included like below.

Include <cstdlib> Header In C++

In C++ atoi() function can be used with the cstdlib header or library. So in order to use atoi() function in C++ we should include this header.

Convert String/Char To Integer In C and C++

We will start with a simple example where we will convert a number in string or char format. In this example, we will convert the “1234” string into an integer. As we can see the “1234” string consist of 4 numbers and can be stored in a int or integer variable.

C:

C++:

Convert String/Char To Negative Integer

In the previous example, we have converted a string which expresses positive number into an integer type. We can also convert or cast a negatively expressed number into an integer. In this example, we will convert “-4321” string into an integer. Keep in mind that there are 5 characters in string presentation of the negative number so the char array or string will be 5 characters long.

C:

C++:

More atoi() Function Examples

We have already examined standard and simple examples about the atoi() function but there may be some complex and hard to understand cases about converting string or char integer into an integer data type.

The output will be like below.

More atoi() Function Examples

More atoi() Function Examples

We can see that there are some rules about converting string or char array into integer where we can list them below.

  • If given string or char array is floating-point like “314.159”  in only integer part will be converted where the result will be “314”
  • If there are some non-numerical chars in the given char array or string they will not convert and there will be no error where only the integer part will be converted. As an example “52345 some text” will be converted 52345
  • If the start of the char array or string and there are numerical characters after them this will be converted into 0 as an integer value. As an example “some text 25” will be converted into 0 .
  • If the numerical characters are between non-numeric characters the conversion result will be 0 too.
  • If the char array or string starts with numeric character and after that, there is non-numeric and then numeric character only the starting numeric character will be converted. As an example “25 text 50” will be converted into 25.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)