简体   繁体   中英

Why is printf not equivalent to scanf?

I have a program which displays "hi", but I do not understand why.

I understand both scanf and printf return the number of characters they read/write but how does it work in this case?

void main()
{
    if(printf==scanf)
        printf("hello");
    else
        printf("hi");
}

You aren't calling the functions and comparing the results, you are comparing the functions themselves , which boils down to comparing the addresses of the functions (function names will convert to function pointers in many contexts, this is one). What you wrote is equal to this:

/* 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");
    }
}

Since scanf and printf are not the same function they live at a different address so the comparison fails and hi is printed.

Here you compare the adreses of the functions and as the functions are not the same, the equality does not hold. I do not see what confuses you.

You are not calling printf or scanf in the if statement. Rather, you are comparing the location of scanf and printf function in the memory, which are different (otherwise, they will run the same code, and have the same functionality).

You only get back the return value if you invoke the function. An invocation will look like <function_name> ( <arguments separated by commas> ) .

As others have already mentioned you are comparing the address of two functions (printf and scanf in this case) and since these functions cannot have the same address, the comparison fails making the program print "hi".

You can try the below code to understand it better

int main(void)
{

   printf("printf = %x\n", printf);
   printf("scanf = %x\n", scanf);

   return 0;
}

因为函数printf的地址与函数scanf

Just to add another angle on this matter. Others have already pointed out how you can compare the addresses to the functions. But this code make (well, it's a far stretch) sense:

int main()
{
    if(printf("")==scanf(""))
        printf("hello");
    else
        printf("hi");
}

This code is guaranteed to print "hello". Why? Well, lets first look at return values. printf will return the number of characters printed, which is zero for an empty string. scanf will return the number of successful assignments (not the number of characters read), which for an empty string is also zero. Well, if some error occurs, it can also return EOF, which I find highly unlikely even if I cannot guarantee that it will not happen. Well, printf can fail too, and will in that case return an unspecified negative number.

So, it will print "hello", unless both printf and scanf both encounters a problem, AND that printf returns EOF, in which it will print "hi" instead.

However, it could be the case that this is UB. I'm not sure if the order of evaluation is well defined, or even if that matters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM