title: Destructor in C?
body: <p>C++ have nice feature allowing to call some code at end of the block - destructor. Yesterday i randomly discovered trick which in C works similarly like destructor in C++. For illustration, here is usage of this trick to automatically close file:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  #define AUTO_FILE( id, name, access ) \
  for ( FILE* id = fopen( name, access ); id != NULL; fclose( id ), id = NULL )
  
  int main()
  {
      AUTO_FILE( f, "hello.txt", "w" ) {
          fprintf( f, "Hello World!" );
      }
      return 0;
  }
  </code></pre>
  
  <p>I think this is nice and can have interesting usages. For example if you combine it with stack data structure you can directly in source code declare very intuitive and readable tree structures:</p>
  
  <pre><code>frame("frm") {
      menu("mnu") {
          popupmenu("file", "File") {
              menuitem("exit", "Exit");
          }
          popupmenu("help", "Help") {
              menuitem("about", "About...");
          }
      }
      label("lbl", "Hello World!");
      button("btn", "Push me!", btn_onclick);
  }
  </code></pre>
  
  <p>But of course this hack don't work like true destructor. If you use <code>break</code> or <code>return</code> statement inside the block, <code>fclose</code> will not be called. But instead you can use <code>continue</code> statement if you need break the block like with <code>break</code> statement in C++. :-) So maybe this trick can be improved with some macro magic to work almost like true destructor.</p>
  
  <p>So question is:</p>
  
  <p>Is it possible to have destructor in standard C programming language?</p>