atoi in cpp code example
Example 1: atoi c++
Parses the C-string str interpreting its content as an integral number
If the converted value would be out of the range of representable
values by an int, it causes undefined behavior.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
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);
return 0;
}
Enter a number: 73
The value entered is 73. Its double is 146.
Example 2: atoi
#include <stdlib.h>
#include <stdio.h>
int main (void)
{
string input = "9";
int output = atoi(input);
printf("%i", output);
}
Example 3: atoi c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
int val;
char str[20];
strcpy(str, "98993489");
val = atoi(str);
printf("String value = %s, Int value = %d\n", str, val);
strcpy(str, "tutorialspoint.com");
val = atoi(str);
printf("String value = %s, Int value = %d\n", str, val);
return(0);
}
Example 4: stoi in c++
stoi()