Whenever we call a function returning value why it is not required to catch the value?
consider the following C code,
int main()
{
int i;
scanf("%d",&i);
printf("Value of i is: ",i);
return 0;
}
Here scanf() returns value 1, but as it is not catched in anywhere why didn’t the error pops up?
What is the reason to allow such programming?
Primarily because expressions in C also yield values. For example:
x = 1;yields the value 1. Sometimes you use that for multiple assignment likex = y = 1;, but more often you don’t.In early C, the
voidreturn type hadn’t been invented either, so every function returned some value, whether it was generally useful or not (for example, your call toprintfalso returns a value).The rules of the language don’t make this an error (doing so would lose compatibility with virtually existing code) and since this is common and rarely indicates a problem, most compilers don’t warning about it either. A few lint tools do, which has led a few misguided programmers to write things like
(void)printf("whatever");(i.e., casting the unused return tovoidto signal that it really, truly was intentional when it was ignored. This, however, rarely does any good, and frequently does quite a bit of harm, so (thankfully) it’s rarely seen.