C examples for stdlib.h:atoi
function
<cstdlib> <stdlib.h>
Convert string to integer
int atoi (const char * str);
Parameter | Description |
---|---|
str | C-string representation of an integral number. |
On success, the function returns the converted integral number.
On error, 0 is returned.
#include <stdio.h> #include <stdlib.h> int main ()/*from ww w. j a v a 2 s. co m*/ { int i; char buffer[256]; printf ("Enter a number: "); fgets (buffer, 256, stdin); i = atoi (buffer); printf ("The value entered is %d. Its double is %d.\n",i,i*2); printf ("%d\n",atoi("0")); printf ("%d\n",atoi("-1")); printf ("%d\n",atoi("-2")); printf ("%d\n",atoi("222")); printf ("%d\n",atoi("123")); printf ("%d\n",atoi("a")); return 0; }