Conversion Between Strings and Numbers
A variety of functions are declared in the header file stdlib.h to obtain numerical interpretations of the initial digit characters in a string. The resulting number is the return value of the function.
-
intatoi( const char *s); Interprets the contents of the string
sas a number with typeint. The analogous functionsatol(),atoll()(*), andatof()are used to convert a string into a number with typelong,long long(*), ordouble.-
doublestrtod( const char *s, char **pptr); Serves a similar purpose to that of
atof(), but takes the address of acharpointer as a second argument. If thecharpointer referenced bypptris not NULL, it is set to the first character in the strings(excluding any leading whitespace) that is not part of the substring representing a floating-point number.The corresponding functions for conversion to the types
floatandlong doublearestrtof()(*) andstrtold()(*).-
longstrtol( const char *s, char **pptr, intbase); Converts a string to a number with type
long. The third parameter is the base of the numeral string, and may be an integer between 2 and 36, or 0. Ifbaseis 0, the stringsis interpreted as a numeral in base 8, 16, or 10, depending on whether it begins with 0, 0x, or one of the digits 1 to 9.The analogous functions for converting a string to
unsigned long,long long(*) orunsigned long long(*) arestrtoul()(*),strtoll()(*), andstrtoull()(*).
The header file inttypes.h
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access