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:


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?


just because a function has a return values, this doesn't mean that you have to use it. per the man page of printf, the value returned represents the number of characters printed (excluding the null byte used to end strings).

What happens to the return values of these functions?

if you don't store it in a variable and/or use it, it's just ignored.

Under which circumstances can we use these functions without putting them in an expression?

you don't have to use any of these functions as part of mathematical expressions unless you need to. so the answer is basically that you do what you need to do.

Update: what you got was not actually an error, but rather, a warning that was treated as an error because of the -Werror option that was passed to clang when compiling your program.

it turns out you may cause a warning to be emitted by the compiler in case the caller does not use the return value of a function as its the case with strlen but not with printf.

you may do that using an attribute called warn_unused_result. See https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Function-Attributes.html to learn more!

  • So I don't have to use it for printf, but I have to use it for strlen? How do we know when we do have to use it then? – Prakrit Duangsutha May 31 '15 at 16:36
  • @PrakritDuangsutha, you don't have to store/use it for strlen as well. – Kareem May 31 '15 at 16:37
  • But I get this: error: ignoring return value of function declared with pure attribute [-Werror, -Wunused-value] – Prakrit Duangsutha May 31 '15 at 16:47
  • It's more about the meaning of the return value. If a function's return value is a calculation based on something given to it, then you almost certainly need to do something with it. Examples are functions like strlen(x), abs(I), etc. They intend to return a value to be used elsewhere. (For some functions, a failure value may be built into the function - see man page). If the function does something, but the return value is about the success of failure of the function to execute, then you probably don't need to do anything with it. Examples are printf(...), fopen( file_to_open), etc. – Cliff B May 31 '15 at 17:33
  • @PrakritDuangsutha take a look at the Update section in the answer above! – Kareem May 31 '15 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .