<p>I long thought that in C, all variables had to be declared at the beginning of the function. I know that in C99, the rules are the same as in C++, but what are the variable declaration placement rules for C89/ANSI C?</p>

<p>The following code compiles successfully with <code>gcc -std=c89</code> and <code>gcc -ansi</code>:</p>

<pre><code>#include &lt;stdio.h&gt;
int main() {
    int i;
    for (i = 0; i &lt; 10; i++) {
        char c = (i % 95) + 32;
        printf("%i: %c\n", i, c);
        char *s;
        s = "some string";
        puts(s);
    }
    return 0;
}
</code></pre>

<p>Shouldn't the declarations of <code>c</code> and <code>s</code> cause an error in C89/ANSI mode?</p>

It compiles successfully because GCC allows the declaration of
```
s
```
as a GNU extension, even though it's not part of the C89 or ANSI standard. If you want to adhere strictly to those standards, you must pass the
```
-pedantic
```
flag.
The declaration of
```
c
```
at the start of a
```
{ }
```
block is part of the C89 standard; the block doesn't have to be a function.