_gcvt() — Convert Floating-Point to String

Format

#include <stdlib.h>
char *_gcvt(double value, int ndec, char *buffer);
Note: The _gcvt function is supported only for C++, not for C.

Threadsafe

Yes

Language Level

Extension

Locale Sensitive

The behavior of this function might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

_gcvt() converts a floating-point value to a character string pointed to by buffer. The buffer should be large enough to hold the converted value and a null character (\0) that _gcvt() automatically adds to the end of the string. There is no provision for overflow.

_gcvt() attempts to produce ndec significant digits in FORTRAN F format. Failing that, it produces ndec significant digits in FORTRAN E format. Trailing zeros might be suppressed in the conversion if they are insignificant.

A FORTRAN F number has the following format:
Read syntax diagramSkip visual syntax diagram+-digit digit . digit
A FORTRAN E number has the following format:
Read syntax diagramSkip visual syntax diagram+-digit. digit E+-digit digit

_gcvt also converts infinity values to the string INFINITY.

Return Value

_gcvt() returns a pointer to the string of digits. If it cannot allocate memory to perform the conversion, _gcvt() returns an empty string and sets errno to ENOMEM.

Example

This example converts the value -3.1415e3 to a character string and places it in the character array buffer1.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char buffer1[10];
    _gcvt(-3.1415e3, 7, buffer1);
    printf("The first result is %s \n", buffer1);
    return 0;
}
The output should be:
       The first result is -3141.5

Related Information