The C language converts numbers to strings.
The C language provides several standard library functions that can convert numbers of any type (integer, long integer, floating point type, etc.) into strings. Here is an example of converting an integer into a string using the itoa () function:
# Include <stdio. h>
# Include <stdlib. h>
Void main (void );
Void main (void)
{
Int num = 100;
Char str [25];
Itoa (num, str, 10 );
Printf ("The number 'num' is % d and the string 'str' is % s. \ n ",
Num, str );
}
The itoa () function has three parameters: the first parameter is the number to be converted, and the second parameter is the target string to be written into the conversion result, the third parameter is the base number used when the number is transferred. In the preceding example, the conversion base is 10.
The following functions can convert integers into strings:
----------------------------------------------------------
Function Name
----------------------------------------------------------
Itoa () converts an integer value to a string
Itoa () converts a long integer value to a string
Ultoa () converts an unsigned long integer value to a string
----------------------------------------------------------
Note that the above functions are not compatible with the ANSI standard. The sprintf () function can be used to convert Integers to strings and is compatible with ANSI standards. See the following example:
# Include <stdio. h>
# Include <stdlib. h>
Void main (void );
Void main (void)
{
Int num = 100;
Char str [25];
Sprintf (str, "% d", num );
Printf ("The number 'num' is % d and the string 'str' is % s. \ n ",
Num, str );
}
When you convert a float number to a string, you need to use another set of functions. The following is an example of converting a floating point value into a string using the fcvt () function:
# Include <stdio. h>
# Include <stdlib. h>
Void main (void );
Void main (void)
{
Double num = 12345.678;
Char * sir;
Int dec_pl, sign, ndigits = 3;/* Keep 3 digits of precision .*/
Str = fcvt (num, ndigits, & dec-pl, & sign);/* Convert the float
To a string .*/
Printf ("Original number; % f \ n", num);/* Print the original
Floating-point
Value .*/
Printf ("Converted string; % s \ n", str);/* Print the converted
String's value .*/
Printf ("Decimal place: % d \ n", dec-pi);/* Print the location
The decimal point .*/
Printf ("Sign: % d \ n", sign);/* Print the sign.
0 = positive,
1 = negative .*/
}
The fcvt () function differs significantly from the itoa () function. The fcvt () function has four parameters: the first parameter is the floating point value to be converted, and the second parameter is the right digit of the decimal point in the conversion result; the third parameter is a pointer to an integer that is used to return the decimal place in the conversion result. The fourth parameter is also a pointer to an integer, this integer is the symbol used to return the Conversion Result (0 corresponds to a positive value, 1 corresponds to a negative value ).
Note that the Conversion Result of the fcvt () function does not really contain the decimal point. Therefore, the fcvt () function returns the decimal place in the conversion result. In the above example, The result value of the integer variable dec_pl is 5, because the decimal point in the conversion result should be placed behind the 5th bits. If you want the Conversion Result to contain a decimal point, you can use the gcvt () function (see the table below ).
The following functions can convert floating point values to strings:
-------------------------------------------------------------------------
Function Name
-------------------------------------------------------------------------
Ecvt () converts a double-precision floating point value to a string. The conversion result does not contain a decimal point.
Fcvt () uses the specified number of digits as the conversion precision, and the remainder is the same as ecvt ()
Gcvt () converts a double-precision floating point value to a string. The conversion result contains a decimal point.