_gcvt
Converts a floating-point value to a string, which it stores in a buffer. A more secure version of this function is available; see _gcvt_s
.
Syntax
char *_gcvt(
double value,
int digits,
char *buffer
);
Parameters
value
Value to be converted.
digits
Number of significant digits stored.
buffer
Storage location for the result.
Return value
_gcvt
returns a pointer to the string of digits.
Remarks
The _gcvt
function converts a floating-point value
to a character string (which includes a decimal point and a possible sign byte) and stores the string in buffer
. The buffer
should be large enough to accommodate the converted value plus a terminating null character, which is appended automatically. If a buffer size of digits
+ 1 is used, the function overwrites the end of the buffer. The overwrite happens because the converted string includes a decimal point and can also contain sign and exponent information. The function doesn't account for the overflow. _gcvt
attempts to produce digits
digits in decimal format. If it can't, it produces digits
digits in exponential format. Trailing zeros might be suppressed in the conversion.
A buffer
of length _CVTBUFSIZE
is sufficient for any floating point value.
This function validates its parameters. If buffer
is NULL
, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, this function sets errno
to EINVAL
and returns NULL
.
By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.
Requirements
Routine | Required header |
---|---|
_gcvt |
<stdlib.h> |
For more compatibility information, see Compatibility.
Example
// crt_gcvt.c
// compile with: /W3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main( void )
{
char buffer[_CVTBUFSIZE];
double value = -1234567890.123;
printf( "The following numbers were converted by _gcvt(value,12,buffer):\n" );
_gcvt( value, 12, buffer ); // C4996
// Note: _gcvt is deprecated; consider using _gcvt_s instead
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
value *= 10;
_gcvt( value, 12, buffer ); // C4996
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
value *= 10;
_gcvt( value, 12, buffer ); // C4996
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
value *= 10;
_gcvt( value, 12, buffer ); // C4996
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
printf( "\n" );
value = -12.34567890123;
_gcvt( value, 12, buffer ); // C4996
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
value /= 10;
_gcvt( value, 12, buffer ); // C4996
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
value /= 10;
_gcvt( value, 12, buffer ); // C4996
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
value /= 10;
_gcvt( value, 12, buffer ); // C4996
printf( "buffer: '%s' (%d chars)\n", buffer, strlen(buffer) );
}
The following numbers were converted by _gcvt(value,12,buffer):
buffer: '-1234567890.12' (14 chars)
buffer: '-12345678901.2' (14 chars)
buffer: '-123456789012' (13 chars)
buffer: '-1.23456789012e+012' (19 chars)
buffer: '-12.3456789012' (14 chars)
buffer: '-1.23456789012' (14 chars)
buffer: '-0.123456789012' (15 chars)
buffer: '-1.23456789012e-002' (19 chars)
See also
Data conversion
Math and floating-point support
atof
, _atof_l
, _wtof
, _wtof_l
_ecvt
_fcvt