将浮点值转换为字符串。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  


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

[in] sizeInBytes[in] sizeInBytes
缓冲区的大小。Size of the buffer.

[in] value[in] value
要转换的值。Value to be converted.

[in] digits[in] digits
存储的有效位数。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

buffer sizeInBytes value digits 返回Return buffer 中的值Value in buffer
NULL 任何any 任何any 任何any EINVAL 未修改。Not modified.
NULL(指向有效内存)Not NULL (points to valid memory) zero 任何any 任何any EINVAL 未修改。Not modified.
NULL(指向有效内存)Not NULL (points to valid memory) 任何any 任何any >= sizeInBytes EINVAL 未修改。Not modified.

安全问题Security Issues

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


_gcvt_s 函数将浮点 value 转换为字符串(它包括小数点和可能的登录字节),并将字符串存储在 buffer 中。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. buffer 应足够大以容纳转换后的值加上一个会自动追加的端接空字符。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. 如果已使用 digits + 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 尝试生成十进制格式的 digits 数字。_gcvt_s attempts to produce digits digits in decimal format. 如果不能,则将生成指数格式的 digits 数字。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 <stdlib.h><stdlib.h> <error.h><error.h>

有关更多兼容性信息,请参见“简介”中的 兼容性For more compatibility information, see Compatibility in the Introduction.


// 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_l atof, _atof_l, _wtof, _wtof_l
_ecvt_s _ecvt_s
_fcvt_s _fcvt_s