Microsoft KB Archive/40027

= INFO: #pragma pack Affects Declarations, Not Definitions =

Article ID: 40027

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft C Professional Development System 6.0a
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.51
 * Microsoft Visual C++ 1.52 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q40027



SUMMARY
The pack pragma affects declarations, not definitions. Place #pragma pack prior to data declarations.



Sample Code
The following program demonstrates usage of #pragma pack: /* compile options needed */ /* Elements of variables of type struct x will be byte-aligned. */ struct x { int a; char b; int c; };
 * 1) pragma pack(1)

/* Elements of variables of type struct y will be word-aligned. */ struct y { int a; char b; int d; };
 * 1) pragma pack(2)

/* The pragma below does NOT affect the definitions that follow. */
 * 1) pragma pack(4)

struct x X; struct y Y;

void main (void) { /* dummy main */ } NOTE: The default packing value for the 16-bit products mentioned above is 2 bytes. The default packing value for Visual C++ 32-bit, versions 1.0 and 2.xx, is 4 bytes. The default packing value for Visual C++ 32-bit, version 4.0, is 4 bytes for bit fields and 8 bytes otherwise.

Keywords: kbinfo kblangc KB40027

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.