1Parses the C-string str interpreting its content as an integral number
2
3If the converted value would be out of the range of representable
4values by an int, it causes undefined behavior.
5
6/* atoi example */
7#include <stdio.h> /* printf, fgets */
8#include <stdlib.h> /* atoi */
9
10int main ()
11{
12 int i;
13 char buffer[256];
14
15 printf ("Enter a number: ");
16 fgets (buffer, 256, stdin);
17 i = atoi (buffer);
18 printf ("The value entered is %d. Its double is %d.\n",i,i*2);
19 return 0;
20}
21
22
23/* Output */
24
25Enter a number: 73
26The value entered is 73. Its double is 146.
27
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5//CONVERT STRING TO INT
6
7int main () {
8 int val;
9 char str[20];
10
11 strcpy(str, "98993489");
12 val = atoi(str);
13 printf("String value = %s, Int value = %d\n", str, val);
14
15 strcpy(str, "tutorialspoint.com");
16 val = atoi(str);
17 printf("String value = %s, Int value = %d\n", str, val);
18
19 return(0);
20}