Reputation: 332
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");
}
Upvotes: 3
Views: 541
Reputation: 31306
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.
Upvotes: 0
Reputation: 24895
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;
}
Upvotes: 2
Reputation: 117681
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.
Upvotes: 13
Reputation: 56809
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> )
.
Upvotes: 3
Reputation: 137312
Because the address of the function printf
is not the same as the function scanf
.
Upvotes: 1
Reputation: 70929
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.
Upvotes: 3