1. Convert string to INTEGER (1)
# include
int atoi (const char * nptr );
string to integer
long atol (const char * nptr );
convert a string to a long integer
long Atoll (const char * nptr );
long atoq (const char * nptr );
converting string to long
the English manual is simple, directly
Note: The atoi () function converts the initial portion of the string pointed to by nptr to int. the behavior is the same as strtol (nptr, (char **) null, 10); records t that atoi () does not detect errors. the atol () and Atoll () functions behave the same as atoi (), doesn't that they convert the initial portion of the string to their return type of long or long. atoq () is an obsolete name for atoll ().
2. Convert string to integer type (2)
# Include <stdlib. h>
Long int strtol (const char * nptr, char ** endptr, int base );
String to a long integer. The base parameter is in hexadecimal format. If the base parameter is in hexadecimal format, the base value is 10.
Long long int strtoll (const char * nptr, char ** endptr, int base );
Convert string to long int
Note: For more information, see the man manual.
3. Convert string to floating point
# Include <stdlib. h>
Double strtodd (const char * nptr, char ** endptr );
String to double-precision floating-point Double Type
Float strtof (const char * nptr, char ** endptr );
Convert string to single-precision floating point numberFloat Type
Long Double strtold (const char * nptr, char ** endptr );
Convert string to longDouble Type
With the above database functions, you can easily convert strings into numeric types. It's really cool and convenient. Is there any wood?
ExampleCode:
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char * str_int = "892 ";
Int int_val = atoi (str_int );
Printf ("Convert string to integer: % d \ n", int_val );
Long long_val = atol (str_int );
Printf ("string to long integer: % LD \ n", long_val );
Char x str_float = "238.23 ";
Char * endptr;
Float float_val = strtof (str_float, & endptr );
Printf ("Convert string to single-precision floating point type: % F \ n", float_val );
Double double_val = strtodd (str_float, & endptr );
Printf ("Convert string to Double Precision Floating Point: % F \ n", double_val );
Char * str_long = "9839282 ";
Int base = 10;
Long long_v = strtol (str_long, & endptr, base );
Printf ("strtol: % LD \ n", long_v );
Return 0;
}
Code output:
- Convert string to integer: 892
- String to a long integer: 892
- Character string to single-precision floating point: 238.229996
- String to double-precision floating point: 238.230000
- Strtol: 9839282
Note: The man manual of each function has a detailed explanation. For more information, see the man manual.