Function fputs always return zero on success I am using the fputs() function to write a string to a file. I want to handle the case in which the string has been successfully written to the file. Here is my code:

#include &lt;stdio.h&gt;

main() {

  FILE *fp;
  int rets; //to keep fputs return value

   fp = fopen("test.txt", "w");
   rets = fputs("\nThis is testing for fputs...\n", fp);
   printf("Fputs return value : %d\n",rets); //Return value is always zero
   fclose(fp);

   if(rets &gt;= 0 )
       printf("Successfully written on the file. Proceed..");

   return 0;
}


According to the documentation for fputs():


  On success, a non-negative value is returned.


When I executed this code multiple times, I noticed that the return value is always zero. According to this observation, it is enough to check if return value is zero (rather than non-negative). According to this, the last line of my code could be:

   if(rets == 0 ) //equal to zero for success
       printf("Successfully written on the file. Proceed..");


Should I trust the above observation?

Is the documentation wrong, or ambiguous?
Although your observations show that you happen to always get 0 as a (success-case) return value, implementations are permitted to return any value of 0 or more.  For example, a different implementation of the standard library may find it convenient to return the number of characters that have been printed.

You cannot assume that rets == 0 will be as good as rets &gt;= 0, because:


You might want to run your code on a different system, with a different standard library.  This might happen if you share your code with somebody else (perhaps for them to help you fix a bug).
The different system might be your current system in a year's time, when you've upgraded your libraries.


If your assumption is no longer true, then you suddenly have a huge number of hard-to-find bugs that all need fixing at once.  That is not what we call a Good Thing.  By depending only on what the standard guarantees, you maximise your likelihood of full portability, including to future versions of your current environment.