c - sprintf() 负返回值和errno

标签 c printf errno

根据 http://linux.die.net/man/3/sprintfhttp://www.cplusplus.com/reference/cstdio/sprintf/ sprintf() 和 family 返回成功写入的字符数。失败时,返回一个负值。我假设如果格式字符串格式错误,则可能会发生错误,因此负返回值可能表示 malloc() 错误之外的其他内容。 errno 是否设置为指示错误是什么?

最佳答案

C++ 遵循 C 并且 C 在 sprintf() 和系列的描述中不需要或提及 errno(尽管对于某些格式说明符,这些函数被定义为调用mbrtowc(),可能会在errno中设置EILSEQ)

POSIX 要求设置错误号:

If an output error was encountered, these functions shall return a negative value and set errno to indicate the error.

明确提及 EILSEQ、EINVAL、EBADF、ENOMEM、EOVERFLOW:http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html

关于c - sprintf() 负返回值和errno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14778123/

相关文章:

IOS BLE scanForPeripheralsWithServices :options: on Background

c - 在 C 语言中,XML 是否是 RPG 引擎内部使用的良好格式?

转换说明符 %ju

c - 使用 errno 进行应用程序/库错误报告

c++ - 调用 fdopen() 后出现 Errno 22

c - 嵌入式系统负载及性能测试

在 COOP 中调用 "method"

printf - 以运行时可选择的精度打印 float

C编程,printf中有空格的正确方法吗?

c++ - 这是使用 strerror_r 的正确方法吗?