YouTip LogoYouTip

C Function Atoi

## C Library Function - `atoi()` The `atoi()` function is a built-in C library function used to convert a string representation of an integer into an actual integer value (`int`). The name stands for **"ASCII to Integer"**. This function is defined in the `` header file. --- ## Syntax & Declaration Below is the prototype of the `atoi()` function: ```c int atoi(const char *str); ``` ### Parameters * **`str`**: A pointer to a null-terminated string that represents an integer value. ### Return Value * **Success**: Returns the converted integer value. * **Failure**: If no valid conversion can be performed (e.g., the string starts with non-numeric characters), the function returns `0`. --- ## How `atoi()` Works The `atoi()` function processes the input string as follows: 1. **Discards leading whitespace**: It automatically skips any leading whitespace characters (spaces, tabs, newlines). 2. **Handles optional signs**: It identifies an optional plus (`+`) or minus (`-`) sign immediately preceding the digits. 3. **Converts digits**: It reads the subsequent characters and converts them to an integer until it encounters a non-digit character. 4. **Stops at non-digits**: Any characters after the valid numeric sequence are ignored. --- ## Code Example The following example demonstrates how to use the `atoi()` function to convert numeric strings, as well as how it handles non-numeric input. ```c #include #include #include int main() { int val; char str; // Example 1: Converting a valid numeric string strcpy(str, "98993489"); val = atoi(str); printf("String value = %s, Integer value = %d\n", str, val); // Example 2: Attempting to convert a non-numeric string strcpy(str, "youtip.co"); val = atoi(str); printf("String value = %s, Integer value = %d\n", str, val); // Example 3: String starting with spaces and a sign strcpy(str, " -42is_the_answer"); val = atoi(str); printf("String value = %s, Integer value = %d\n", str, val); return 0; } ``` ### Output When you compile and run the program above, it produces the following output: ```text String value = 98993489, Integer value = 98993489 String value = youtip.co, Integer value = 0 String value = -42is_the_answer, Integer value = -42 ``` --- ## Important Considerations & Limitations While `atoi()` is simple and easy to use, it has several limitations that developers should be aware of: ### 1. No Error Detection If `atoi()` encounters an invalid string (like `"abc"`), it returns `0`. This makes it impossible to distinguish between a failed conversion and a successful conversion of the string `"0"`. ### 2. Overflow Behavior is Undefined If the converted value falls outside the range of representable values for an `int` (typically `-2,147,483,648` to `2,147,483,647` on 32-bit systems), the behavior is **undefined**. It may wrap around, return a garbage value, or cause other unexpected issues. ### 3. Modern Alternatives: `strtol()` For robust applications, it is highly recommended to use **`strtol()`** (string to long) instead of `atoi()`. `strtol()` provides: * **Error checking**: It allows you to detect if the conversion succeeded or failed. * **Overflow protection**: It sets the global `errno` variable to `ERANGE` if the value is out of bounds. * **Base flexibility**: It supports conversions for binary, octal, decimal, and hexadecimal strings.
← C Function AtolC Function Atof β†’