title: using a class type in printf()
body: <p>I am experimenting to change some of the current code in a library that returns an enum for status code to a class type that has status and sub-status as shown below in <em>status</em> class below. One of the requirements is to have this work with lot of existing code that uses the type in == and != kind of checks all over the code base. Another requirement is to be able to use it existing printf statements all over the place.</p>
  <p>I converted the enum to #define as below and then used operator overloading for == (will have to implement inequality later). I was expecting the printf() usage shown below to fail when I try to print <em>status</em>. However, surprisingly that seems to be working and printing the status_ member field value already !! How is it working ? Can someone please help make it make sense ?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  // these are the status codes that any function can return
  // for example test_function() below returns one of these
  
  #define STATUS_OK    100
  #define STATUS_ERR   200
  
  // this is the new class that replaces the enum types
  class status {
  public:
      status() {}
      status(unsigned int status) : status_(status) {}
      status(unsigned int status, unsigned int sub_status) : status_(status), sub_status_(sub_status) {}
      bool operator==(const status&amp; other) {
          return (status_ == other.status_);
      }
  
  private:
      unsigned int status_;
      unsigned int sub_status_;  // sub_status_ is meant for future usage
  };
  
  // helper function to test the main code
  // this returns possible status codes
  status
  test_function (void)
  {
      int r1, r2;
  
      r1 = rand();
      r2 = rand();
      if (r1 &gt; r2) {
          return STATUS_OK;
      }
      return STATUS_ERR;
  }
  
  int
  main ()
  {
      status ret;
  
      srand(time(0));
      ret = test_function();
      printf(&quot;ret is %u\n&quot;, ret);   // how is this already working ?? %u should expect unsigned int ??
      if (ret == STATUS_OK) {
          printf(&quot;ret is OK\n&quot;);
      } else {
          printf(&quot;ret is not OK\n&quot;);
      }
      return 0;
  }
  </code></pre>
  <p>A sample runs print the following:</p>
  <pre><code># ./a.out
  ret is 200.  &lt;== what makes this work ? how is class type getting converted to integer?
  ret is not OK
  # ./a.out
  ret is 100
  ret is OK
  </code></pre>
  <p>As a follow up question, is there anything in the status class that I can do to make printf() legitimately work work this way ? This is convenient as I can avoid touching lot of code.</p>