I just figured out that printf
is a function of type int
.
int printf(const char* format, ...);
And functions are supposed to return a value.
I've never seen a line of code written like strlen("hello");
before. That's like writing 5;
in the code. Useless? Yes. The compiler yelled at me. 5
is not a command. That's an unused value. To solve the problem, we try to use the return value of a function by assigning it to a variable, like this:
int n = strlen("helllo");
However, I don't see the compiler yelling at me for not using the return value of printf
. We can literally write it like this:
printf("hello\n");
Upon further observation, I realized that even though some functions have return values, we do not need to use them if that function also does something else beside working with values.
Some examples include printf
, fread
and fseek
.
Can anyone explain to me please? What happens to the return values of these functions?
Under which circumstances can we use these functions without putting them in an expression?