#error "This compiler does not conform to the ANSI C standard."
#endif
15.7 #pragma命令
#pragma
命令是向编译器提供额外信息的标准方法,其格式如下:
#pragma [
tokens
]
如果
#pragma
之后的第一个标记(
token
)是
STDC
,那么该命令就是一个标准
pragma
。否
则,该
#pragma
命令的作用取决于实现版本。为了保障代码的可移植性,应该尽量少使
用
#pragma
命令。
如果预处理器支持所指定的标记,就会执行这些标记所代表的动作,或者把该信息传递
给编译器。如果预处理器不支持所指定的标记,就忽略该
#pragma
命令。
例如,最新版本的
GNU C
编译器和微软
Visual C
编译器都支持
#pragma pack(
n
)
,它
使得
编译器让结构成员对齐到特定的字节边界。下面的例子使用
pack(1)
指示每个结构成员
必须对齐到字节边界:
#if defined( __GNUC__ ) || defined( _MSC_VER )
#pragma pack(1) //
对齐字节,没有填充
#endif
单字节对齐方式可以确保结构成员之间不会有间隙。
pack
的参数
n
通常是
2
的幂(但幂值
较小)。例如,
pack(2)
把结构成员对齐到偶数地址,而
pack(4)
把
结构成员对齐到
4
为
倍数的地址。
pack()
没
有参数,它指示对齐方式设置为实现版本的默认值。 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.