Q:

Can someone explain variables declaration in C

I'm new to programming, I'm taking this online programming course CS50. So I had an assignment to write a program in C, where user inputs some words (no matter how much space there is before or after words) and we have to print first initials of each word. So I made this program:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>

  int main(void)
  {
  int n;
  int i;
  string name = get_string();
  if(name != NULL)
  {
      if (name[0] != ' ')
      {
          printf("%c", toupper(name[0]));
      }
      for(i = 0, n = strlen(name); i < n; i++)
      {
          if(name[i] ==' ' && isalpha(name[i+1]))
          {
              printf("%c", toupper(name[i+1]));
          }
      }printf("\n");
    }
  }

But it was correctly done only after I declared variables int n; int i;
Before that, I could not even compile program. Why? At first I declared int i
in for loop, but program didn`t even compiled. And just out of luck I tried to declare outside loop and its correct. I dont understand this point. Can someone explain? :)

A:

All variables and functions must be declared before they may be used.  The variable i must be declared before it can be used as an index in the for loop.
Under the 1989/1990 standard and earlier K&R language versions, all  declarations had to come before any executable statements in a block:
void foo( void )
{
  /** 
   * The variable i is used to control a for loop later on in the function,
   * but it must be declared before any executable statements.
   */
  int i;

  /**
   * Some amount of code here
   */

  for( i = 0; i < some_value; i++ ) // K&R and C90 do not allow declarations within the loop control expression
  {
    /**
     * The variable j is used only within the body of the for loop.
     * Like i, it must be declared before any executable statements
     * within the loop body.
     */
    int j;

    /**
     * Some amount of code here 
     */
    j = some_result();

    /**
     * More code here
     */
    printf( "j = %d\n", j );
  }
}

As of the 1999 standard, declarations may be mixed with other statements, and they may appear as part of the initial expression of a for loop:
void foo( void )
{
  /**
   * Some amount of code here
   */
  for ( int i = 0; i < some_value; i++ ) // C99 and later allow variable declarations within loop control expression
  {
    /**
     * Some code here
     */
    int j = some_result(); // declare j when you need it
    /**
     * More code here
     */
    printf( "j = %d\n", j );
  }
}

The chief difference between the two snippets above is that in the first case, i is visible over the body of the entire function, whereas in the second snippet, it's only visible within the body of the for loop.  If you need i to be visible to any code following the for loop, then you need to declare it outside the loop control expression:
int i;
for ( i = 0; i < some_value; i++ )
{
  ...
}
...
do_something_with( i );

Again, i must be declared before it can be used in the loop control expression; it's just in the second case, that declaration is part of the loop control expression.  
EDIT
I don't know what development environment or compiler you are using.  You might want to see if you can specify which version of the language you want to compile against (for example, in gcc, you'd specify -ansi or -std=c90 for the 1989/1990 version and -std=c99 for the 1999 version).  

