在 C 语言中实现/定义的格式说明符在哪里?

Where are the format specifiers implemented/defined in the C language?

提问人:Bishop 提问时间:8/11/2023 最后编辑:user16217248Bishop 更新时间:8/11/2023 访问量:102

问:

我已经在 C 语言中工作了几个月,我从来没有想过要检查在 C 语言格式说明符中实际实现/定义的格式说明符的位置,例如:

%f
%d
%p
%s
#include <stdio.h>

int main(){
    printf("%f",7.8);
}

我知道这些格式说明符是用来的,但我的问题是它们在 C 中在哪里实现/定义?stdio.h

C printf 格式说明符

评论

4赞 tadman 8/11/2023
看哪:文档
2赞 klutt 8/11/2023
你试过查看文件吗?stdio.h
1赞 zwol 8/11/2023
sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/......
4赞 Barmar 8/11/2023
@klutt 它们不会在头文件中,而是在 的源代码中。printf()
1赞 Eugene Sh. 8/11/2023
它们是作为使用它们(和变体)的函数的一部分实现的。printfscanf

答:

3赞 KamilCuk 8/11/2023 #1

格式说明符在哪里实现

它们是在您正在使用的特定实现源代码中实现的。有许多 prinf 的实现和 C 标准库的实现 - https://en.wikipedia.org/wiki/C_standard_library#Implementations。您正在使用其中之一printf

有些更容易阅读 - https://github.com/eblot/newlib/blob/master/newlib/libc/stdio/vfprintf.c#L1220 .有些很难理解,并且已经优化了几十年 - https://github.com/lattera/glibc/blob/master/stdio-common/vfprintf.c#L293 .

用 C 语言定义?

printf格式说明符在 C 语言标准中被“定义”——标准化、指定。我们在线使用的标准草案,您可以在此处阅读有关它们的信息 https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p1 .但是 cppreference https://en.cppreference.com/w/c/io/fprintf 比干标准更友好,格式也更好阅读。