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)
Code Example |
---|
C :: random number c |
C :: see if two strings are equal in C |
C :: lsusb command not found |
C :: addition of two matrix in c |
C :: how to shutdown system c++ |
C :: printf c float |
C :: find the largest number among five numbers in c language |
C :: c 2d array dimensions |
C :: how to mutex lock in c |
C :: c printf uint32_t |
C :: create empty vector in rust |
C :: matplotlib plot circle marker |
C :: round function in c |
C :: typedef pointer |
C :: how to scan in c |
C :: goto statement in c |
C :: factorial of a number in c |
C :: fgets function in c |
C :: putchar in c |
C :: c read file content |
C :: signal function c |
C :: latex remove page number from footer |
C :: dynamic memory allocation c |
C :: continue statement in c |
C :: How to copy one string into another in C |
C :: binary tree count number of leaves in c |
C :: division en java |
C :: snprintf c |
C :: check for duplicates c |
C :: c pointers and arrays |