ft_atoi

Subject

ATOI(3) (simplified)

NAME
    atoi -- convert ASCII string to integer
SYNOPSIS
    int atoi(const char *str);
DESCRIPTION
    The atoi() function converts the initial portion of the string pointed to by str to int representation.

Understandable explanation

The atoi() function converts a string to its int representation.

Some things that the atoi() function does are not clearly said in the man. I'll quickly list them here.

  • The string passed as parameter may begin with an arbitrary number of whitespaces as determined by isspace(3)

  • After the arbitrary number of whitespaces, there can be one single optional '+' or '-' sign

  • The remainder of the string will be converted to an int, stopping at the first character which is not a valid digit in the given base (in our case we only need to manage base 10, so the valid digits are 0-9)

I talked about the isspace(3) function, what is that function ? It works the same way as the isdigit, isalpha, etc. but returning a non-zero value when the character is one of the following

isspace(3)
  • \t => tabulation

  • \n => new line

  • \v => vertical tabulation

  • \f => form feed

  • \r => carriage return

  • ' ' => space

To make it easier, will be coding the isspace(3) function as a static helper function for our atoi(3) function.

Hints

Commented solution

ft_atoi
Conversion example

I hope this will help you understand what happens in the while loop of our ft_atoi() function.

Last updated

Was this helpful?