title: Scope of the pragma pack directive in Visual Studio
body: <p>What is the scope of the <code>#pragma pack</code> alignment in Visual C++? The API reference
  <a href="https://msdn.microsoft.com/en-us/library/vstudio/2e70t5y1%28v=vs.120%29.aspx" rel="noreferrer">https://msdn.microsoft.com/en-us/library/vstudio/2e70t5y1%28v=vs.120%29.aspx</a>
  says:</p>
  
  <blockquote>
    <p>pack takes effect at the first struct, union, or class declaration
    after the pragma is seen</p>
  </blockquote>
  
  <p>So as a consequence for the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #pragma pack(push, 1)
  
  struct FirstExample
  {
     int intVar;   // 4 bytes
     char charVar; // 1 byte
  };
  
  struct SecondExample
  {
     int intVar;   // 4 bytes
     char charVar; // 1 byte
  };
  
  
  void main()
  {
     printf("Size of the FirstExample is %d\n", sizeof(FirstExample));
     printf("Size of the SecondExample is %d\n", sizeof(SecondExample));
  }
  </code></pre>
  
  <p>I've expected:</p>
  
  <pre><code>Size of the FirstExample is 5
  Size of the SecondExample is 8
  </code></pre>
  
  <p>but I've received:</p>
  
  <pre><code>Size of the FirstExample is 5
  Size of the SecondExample is 5
  </code></pre>
  
  <p>That is why I am a little surprised and I really appreciate any explanation you can provide.</p>