Pointer to a null-terminated string representing an integer value
Read more about parameters of atoi in parameters section
The atoifunction in C language Converts a string to an integer.
It skips any whitespace characters at the beginning of the string, then takes an optional plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. Important: atoi function does not recognize decimal point as it , but rather treats it as any other non-digit character and breaks the loop.
To Summarize the Workflow:
1.
Takes a string (like "123") as input.
2.
Scans the string, ignoring leading whitespace.
3.
If plus or minus sign is present before digits begin- processes it as a sign.
4.
In case no sign found-sets result to positive as default
5.
Starts looping over following characters. In case those represent digits-processes digits.
6.
Stops at the first non-digit character (like a letter or symbol).
7.
Returns the integer value of the processed digits (e.g., 123).
8.
If the string doesn’t start with a valid number, it returns 0.
Read more about return type and value ofatoi function in return section.
The atoifunction takes 1
parameter:
•
const char * `str`: Pointer to a null-terminated string representing an integer value
Converts the initial portion of a string to an `int` value, interpreting the content as a decimal integer.
The atoi function return value :
Returns the converted integer value
If no valid conversion could be performed, it returns 0
Output
This example demonstrates the basic usage of the `atoi` function with different input strings, including a positive number, a negative number with leading whitespace, a number followed by non-numeric characters, and an invalid input.