ATOI Function In C - Linux Hint
Maybe your like
The atoi function is part of the C standard library. Its primary use is to parse a string and convert its contents to the corresponding numerical value of int type.
This tutorial will discuss how to use the atoi function to convert strings to integer values in C.
Basic Usage
The syntax for the atoi function is:
int atoi(const char *str);This function accepts a single parameter, which is a pointer to the string to convert. This value is a constant; thus, the function does not alter the original string.
The function returns the converted string to its equivalent integer type.
How it Works
The atoi function works by removing all possible whitespace characters—similar to the isspace function—until it encounters the first non-whitespace character.
After the first non-whitespace, it assigns an optional sign (positive or negative). Next, it parses all the possible base-10 values until it encounters a non-numerical character. This could be a null-terminating character.
Finally, it interprets the values into their corresponding integer type.
Atoi Function Example
The function below shows how to use the atoi function in C.
#include <stdio.h> #include <stdlib.h> int main() { int i; char str[100]; printf("Enter a number: "); fgets(str, 100, stdin); i = atoi(str); printf("i is %d", i); return 0; }The program above asks the user to provide a number and reads the value from stdin. Next, we parse its contents to an integer using the atoi function.
The result is:
Enter a number: 232 i is 232Conclusion
This quick tutorial has walked you through how to use the atoi function to convert a string to an integer in C.
Tag » What Is Atoi In C
-
C Language: Atoi Function (Convert String To Integer) - TechOnTheNet
-
C Library Function - Atoi() - Tutorialspoint
-
Write Your Own Atoi() - GeeksforGeeks
-
Atoi() — Convert Character String To Integer - IBM
-
Atoi Is A Function In The C Programming Language That ... - Wikibooks
-
Atoi() Function In C - Javatpoint
-
[C언어/C++] Atoi, Atof, Atol 함수 (char* To Int) - 개발자 지망생
-
Atoi - Convert String To Integer
-
Atoi In C++: An Ultimate Guide To Atoi() Function - Simplilearn
-
What Is An Atoi Function In C Programming? - Quora
-
Where Did The Name `atoi` Come From? - Stack Overflow
-
C Atoi() Function With Examples | Learn ETutorials
-
- Atoi()
-
Atoi