简体   繁体   English

在哪些情况下 printf 返回负值?

[英]In which cases does printf return a negative value?

I'v been working on my own version of printf for educational purposes and while reading the manual I read that the function can return negative value when facing an error.出于教育目的,我一直在开发自己的 printf 版本,在阅读手册时,我读到 function 在遇到错误时可以返回负值。

At first I believed that it returns -1 / negative value when an error occurs in the format string but this is not the case.起初我认为当格式字符串中发生错误时它会返回 -1 / 负值,但事实并非如此。

I began to think that it returns -1 when it encounters some syscall error and doesn't succeed in writing to the file descriptor, then I tested this code to check this idea:我开始认为它在遇到一些系统调用错误并且没有成功写入文件描述符时返回-1,然后我测试了这段代码来验证这个想法:

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    errno = 0;
    int stdout2 = dup(STDOUT_FILENO);
    close(STDOUT_FILENO);
    int ret = printf("Hey", "Hello");
    fprintf(stderr, "%d\t%s\n", ret, strerror(errno));
}

It outputted:它输出:

3       Bad file descriptor

Errno is set, there is actually an error (the file descriptor is close) but no negative values are outputted.设置了 Errno,实际上有错误(文件描述符关闭)但没有输出负值。

I'm on Linux and use the glibc, but I also encountered this case in MacOS.我在 Linux 上并使用 glibc,但我在 MacOS 中也遇到过这种情况。 Any idea?任何想法?

In which case printf return a negative value?在哪种情况下 printf 返回负值?

The C standard says (C99 draft): C 标准说(C99 草案):

The printf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred printf function 返回传输的字符数,如果发生 output 或编码错误,则返回负值

The POSIX standard says: POSIX 标准说:

If an output error was encountered, these functions shall return a negative value and set errno to indicate the error.如果遇到 output 错误,这些函数应返回负值并设置 errno 以指示错误。

So i read all your comments and thanks to Peter i closed stdoud like this所以我阅读了你所有的评论,感谢彼得,我像这样关闭了标准

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    errno = 0;
    fclose(stdout);
    int ret = printf("Hey", "Hello");
    fprintf(stderr, "%d\t%s\n", ret, strerror(errno));
}

And it finally returns -1.它最终返回-1。 But i'm surprised that printf does'nt check the values of system calls and threat them as error when they are (because when i close the file descriptor directly the printf does'nt work and set errno to Bad File Descriptor but return the number of characters and not an error).但是我很惊讶 printf 不检查系统调用的值并在它们出现时将它们威胁为错误(因为当我直接关闭文件描述符时 printf 不起作用并将 errno 设置为 Bad File Descriptor 但返回数字字符而不是错误)。 Thanks for all of your answers.感谢您的所有回答。 And sorry if i left some messy code i was just testing at 4am haha.抱歉,如果我留下了一些我刚刚在凌晨 4 点测试的凌乱代码哈哈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在哪些情况下(const)引用返回值是不安全的? - In which cases is it unsafe to (const) reference a return value? 在哪些情况下,将限制限定符应用于返回值会产生影响? - In which cases will the restrict qualifier applied to a return value have an effect? 在什么情况下不会执行vtable构造? - In which cases does vtable construction not occur? 为什么numeric_limits :: min为int返回负值但是为float / double返回正值? - Why does numeric_limits::min return a negative value for int but positive values for float/double? 在堆上分配时,在什么情况下(如果有的话)会发生“自动”清理,而在什么情况下不会发生? - When allocating on the heap, in which cases does “automatic” cleanup occur (if at all) and in which cases does it not? 如何从 function 返回负值? - How to return negative value from function? 为什么printf(“%。2f”,(double)12.555)返回12.55? - Why does printf(“%.2f”, (double) 12.555) return 12.55? 在某些情况下,将auto用作返回类型和返回值nullptr - Using auto as return type and return value nullptr in some cases 是否存在无法避免lambda中的trailing-return-type语法的情况? - Are there cases in which trailing-return-type syntax in lambda cannot be avoided? 为什么在printf负整数值时需要强制类型转换 - why do I need type casting when printf negative interger value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM