title: Declared class inside the main() function in C++
body: <p>In the following program, I have declared class inside the main() function. </p>
  
  <p><strong>case 1:</strong></p>
  
  <pre><code>int main()
  {
          static int i = 10; // static variable
  
          class A
          {
          public:
                  A()
                  {
                      std::cout&lt;&lt;i;
                  }
          };
          A a;
          return 0;
  }
  </code></pre>
  
  <p>and It's working fine in <strong>G++</strong> compiler. </p>
  
  <p>But, If I remove <code>static</code> keyword and compile it, compiler gives an error.</p>
  
  <p><strong>Case 2:</strong></p>
  
  <pre><code>int main()
  {
          int i = 10; // removed static keyword
  
          class A
          {
          public:
                  A()
                  {
                      std::cout&lt;&lt;i;
                  }
          };
          A a;
          return 0;
  }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code> In constructor 'main()::A::A()':
  13:32: error: use of local variable with automatic storage from containing function
  :cout&lt;&lt;i;
                                  ^
  7:13: note: 'int i' declared here
           int i = 10;
               ^
  </code></pre>
  
  <p><strong>Why case 1 working fine? and Why doesn't working case 2?</strong></p>