atoi(str) is unsafe
This is the prefered method:
(int)strtol(str, (char **)NULL, 10)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch[]="123";
int a=atoi(ch);
printf("%d",a);
return 0;
}
char s[] = "45";
int num = atoi(s);
int atoi(const char *string)
use atoi from the stdlib.h
char *string = "hi";
int x = atoi(string);
// C program to demonstrate
// the working of SSCANF() to
// convert a string into a number
#include <stdio.h>
int main()
{
const char* str = "12345";
int x;
sscanf(str, "%d", &x);
printf("
The value of x : %d", x);
return 0;
}
const char *number = "10";
char *end;
long int value = strtol(number, &end, 10);
if (end == number || *end != ' ' || errno == ERANGE)
printf("Not a number");
else
printf("Value: %ld", value);
int atoi(const char *string)
int atoi(const char *string)
int atoi(const char *string)
int atoi(const char *string)