为什么printf不等同于scanf?

Aki*_*a91 1 c

我有一个显示"hi"的程序,但我不明白为什么.

我理解scanf和printf都会返回它们读/写的字符数,但在这种情况下它是如何工作的?

void main()
{
    if(printf==scanf)
        printf("hello");
    else
        printf("hi");
}
Run Code Online (Sandbox Code Playgroud)

orl*_*rlp 13

您没有调用函数并比较结果,您正在比较函数本身,这可以归结为比较函数的地址(函数名称将转换为许多上下文中的函数指针,这是一个).你写的是等于这个:

/* this is the correct signature for main by the way, not `void main()` */
int main(int argc, char **argv) {
    /* compare the address of printf to that of scanf */
    if (&printf == &scanf) {
        printf("hello");
    } else {
        printf("hi");
    }
}
Run Code Online (Sandbox Code Playgroud)

由于scanfprintf是不一样的功能,他们居住在不同的地址,以便比较失败和hi被打印.