将浮点值转换为字符串。Converts a floating-point value to a string. 这是 _gcvt 版本,具有 CRT 中的安全功能所述的安全增强功能。This is a version of _gcvt with security enhancements as described in Security Features in the CRT.


errno_t _gcvt_s(
   char *buffer,
   size_t sizeInBytes,
   double value,
   int digits
template <size_t cchStr>
errno_t _gcvt_s(
   char (&buffer)[cchStr],
   double value,
   int digits
); // C++ only


存储转换结果的缓冲区。Buffer to store the result of the conversion.

缓冲区的大小。Size of the buffer.

要转换的值。Value to be converted.

存储的有效位数。Number of significant digits stored.

返回值Return Value

如果成功,则返回 0。Zero if successful. 如果由于无效参数导致失败(请参阅下表中的无效值),则调用无效参数处理程序,如参数验证中所述。If a failure occurs due to an invalid parameter (see the following table for invalid values), the invalid parameter handler is invoked as described in Parameter Validation. 如果允许执行继续,则返回错误代码。If execution is allowed to continue, an error code is returned. 错误代码是在 Errno.h 中定义的。Error codes are defined in Errno.h. 有关这些错误的列表,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerrFor a listing of these errors, see errno, _doserrno, _sys_errlist, and _sys_nerr.

错误条件Error Conditions

bufferbuffer sizeInBytessizeInBytes valuevalue digitsdigits 返回Return 中的值缓冲区Value in buffer
NULLNULL 任何any 任何any 任何any EINVALEINVAL 未修改。Not modified.
NULL (指向有效内存)Not NULL (points to valid memory) zero 任何any 任何any EINVALEINVAL 未修改。Not modified.
NULL (指向有效内存)Not NULL (points to valid memory) 任何any 任何any >= sizeInBytes>= sizeInBytes EINVALEINVAL 未修改。Not modified.

安全问题Security Issues

_gcvt_s如果可以生成访问冲突缓冲区不指向有效内存且不NULL_gcvt_s can generate an access violation if buffer does not point to valid memory and is not NULL.


_Gcvt_s函数将转换浮点转换为字符串 (其中包括小数点和可能的登录字节),并将存储中的字符串缓冲区.The _gcvt_s 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. 缓冲区应足够大以容纳转换后的值加上会自动追加终止 null 字符。buffer should be large enough to accommodate the converted value plus a terminating null character, which is appended automatically. 缓冲区长度 _CVTBUFSIZE足以满足任何浮点值。A buffer of length _CVTBUFSIZE is sufficient for any floating point value. 如果缓冲区大小为位数+ 1,则该函数将不会覆盖结束时的缓冲区,因此请确保提供足够的缓冲区用于此操作。If a buffer size of digits + 1 is used, the function will not overwrite the end of the buffer, so be sure to supply a sufficient buffer for this operation. _gcvt_s尝试生成数字以十进制格式的数字。_gcvt_s attempts to produce digits digits in decimal format. 如果不能则会生成位数指数格式的数字。If it cannot, it produces digits digits in exponential format. 在转换过程中,可以取消零结尾。Trailing zeros can be suppressed in the conversion.

在 C++ 中,通过模板重载简化此函数的使用;重载可以自动推导出缓冲区长度,不再需要指定大小参数。In C++, using this function is simplified by a template overload; the overload can infer buffer length automatically, eliminating the need to specify a size argument. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

此函数的调试版本首先使用 0xFD 填充缓冲区。The debug version of this function first fills the buffer with 0xFD. 若要禁用此行为,请使用 _CrtSetDebugFillThresholdTo disable this behavior, use _CrtSetDebugFillThreshold.


例程所返回的值Routine 必需的标头Required header 可选标头Optional header
_gcvt_s_gcvt_s <stdlib.h><stdlib.h> <error.h><error.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.


// crt_gcvt_s.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main()
    char buf[_CVTBUFSIZE];
    int decimal;
    int sign;
    int err;

    err = _gcvt_s(buf, _CVTBUFSIZE, 1.2, 5);

    if (err != 0)
        printf("_gcvt_s failed with error code %d\n", err);

    printf("Converted value: %s\n", buf);
Converted value: 1.2

请参阅See also

数据转换Data Conversion
浮点支持Floating-Point Support
atof、_atof_l、_wtof、_wtof_latof, _atof_l, _wtof, _wtof_l