The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.
The following functions can be used to convert strings to numbers:
Function Name Purpose
atof() Converts a string to a double-precision floating-point value.
atoi() Converts a string to an integer.
atol() Converts a string to a long integer.
strtod() Converts a string to a double-precision floating-point value and reports any ?leftover? numbers that could not be converted.
strtol() Converts a string to a long integer and reports any ?leftover? numbers that could not be converted.
strtoul() Converts a string to an unsigned long integer and reports any ?leftover? numbers that could not be converted.
You can convert an string to an number.
#include<stdio.h> #include<conio.h> void main() { char ch[20]; int No; clrscr(); printf("Enter the string which contain the No.:"); scanf("%s",&ch); No=atoi(ch); printf("Integer No.=%d",No); getch(); }
Use sscanf to convert if you are sure that the string contains only digits.
#include<stdio.h>
int main() { char buf[20]; int num; printf("Enter the string which contains integer number :"); scanf("%s", buf); sscanf(buf, "%d", &num); printf("Integer Num = %d
", num); return (0); }