3

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

6 回答 6

13

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.

于 2012-05-29T12:00:20.830 回答
3

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.

于 2012-05-29T11:59:47.807 回答
3

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> ).

于 2012-05-29T12:03:38.880 回答
2

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;
}
于 2012-05-29T12:16:57.947 回答
1

Because the address of the function printf is not the same as the function scanf.

于 2012-05-29T11:59:48.103 回答
0

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.

于 2020-05-05T20:58:09.520 回答