title: Optimal declaration of variables with regards to scope
body: <p>I ask this question mostly in regards to C programming, but insights on any language are welcome.</p>
  
  <p>When it comes to C, I know it only lets variable declarations occur at the very beginning of a block of code. And I have been under the impression that one should declare all variables to be used within a function at the very beginning of the function. But there are many occasions where I'll have a variable that is only used within a loop (or similar block).</p>
  
  <p>an example would be a temporary variable for some return value:</p>
  
  <pre><code>while ( whatever ) {
    int ret;
    ret = getSomeValue();
  }
  </code></pre>
  
  <p>or where some state may need to be held:</p>
  
  <pre><code>while ( whatever ) {
    static int count=0;
    count++;
  }
  </code></pre>
  
  <p>I was wondering if it is considered improper, or, if there is any negative impact to declaring variables within control flow blocks such as if-else, for loops, while loops, etc.</p>
  
  <p>Should variables always be declared with the tightest possible scope? what about for static declaration?</p>
  
  <p><strong>Edit</strong>:
  Okay, I probably should have said I'm aware C99 is more liberal when it comes to where you declare variables, but from a lot of C code I see, they're still usually declared at top. Also, I use VS2K8 which still complains about declarations.</p>
  
  <p>Also, seeing as I've had 2 votes to close this thead, I'll make it clear that I'm more concerned with the performance and compiler aspects than I am with anything stylistic.</p>