This is done by using the function named as atoi(). This function atoi() is present in the header file named as <stdlib.h>. So when programmers use this function they must include the header file by the statement
#include <stdlib.h>
The syntax for this function is given by
int atoi( const char *s)
Here the function atoi() converts the string pointed by s into integer and the return data type is therefore int. The function atoi() starts with string which may have numeric and non numeric value and the function atoi gets terminated when it encounters a non numeric character in the string. So for getting output from this function it must have at least a numeric character starting in the string.
Let us consider the above function by means of an example:
main()
{
int a;
a= atoi(“317.890”);
}
So in the program the function atoi() converts starts with numeric character 3 and proceeds till 317 and terminates on encountering the non numeric character “. “ and therefore a gets value as 317.
How to convert a string into an integer in C program?
This is done by using the function named as atoi(). This function atoi() is present in the header file named as <stdlib.h>. So when programmers use this function they must include the header file by the statement
The syntax for this function is given by
Here the function atoi() converts the string pointed by s into integer and the return data type is therefore int. The function atoi() starts with string which may have numeric and non numeric value and the function atoi gets terminated when it encounters a non numeric character in the string. So for getting output from this function it must have at least a numeric character starting in the string.
Let us consider the above function by means of an example:
So in the program the function atoi() converts starts with numeric character 3 and proceeds till 317 and terminates on encountering the non numeric character “. “ and therefore a gets value as 317.
Profile Answers by GeekAdmin Questions by GeekAdmin
Questions by GeekAdmin answers by GeekAdmin
Related Answered Questions
Related Open Questions