关于pragma命令的使用

#pragma message(“prompt”)

#include <stdio.h>
int main(int argc , char *argv)
{
    
    
	printf("hello world!\n");
	#pragma message("hello c !")
	return 0 ;
}

在codebloks编译的结果为:

-------------- Build: Debug in define_undef (compiler: GNU GCC Compiler)---------------

mingw32-gcc.exe -Wall -g  -c D:\desktop\CODEBLOKSfile\define_undef\main.c -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\define_undef.exe obj\Debug\main.o   
D:\desktop\CODEBLOKSfile\define_undef\main.c: In function 'main':
D:\desktop\CODEBLOKSfile\define_undef\main.c:7:13: note: #pragma message: hello c !
     #pragma message("hello c !")
             ^
Output file is bin\Debug\define_undef.exe with size 28.47 KB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

所以,message 打印消息出现在编译的时候,不会出现在程序最终的运行结果中。

#pragma once

如果在头文件的开头部分加入这条指令,那么就能保证头文件只被编译一次。 可以避免名字冲突,相较于#ifndef 来说兼容性较差。

#pragma hdrstop

该指令表示编译头文件到此为止,后面的无需再编译了。

#pragma pack()

int main(int argc,char*argv[])
{
    
    
    #pragma pack(2)
    struct _stu1{
    
    
        char name[20];
        int score;
        char sex;
    }stu1;
    printf(" str1 占用内存的大小为:%d 个字节\n ",sizeof(stu1));
    #pragma pack()
    struct _stu2{
    
    
        char name[20];
        int score;
        char sex;
    }stu2;
    printf("str2 占用内存的大小为:%d 个字节\n",sizeof(stu2));
    return 0;
}

在结构体st1 的前面使用了#pragma pack(2),其作用是设置2 字节对
齐,接下来使用了#pragma pack(),其作用是取消之前设置的字节对齐方式,采用默认的4
字节对齐。代码运行结果为:

str1 占用内存的大小为:36 个字节
str2 占用内存的大小为:40 个字节

Process returned 0 (0x0)   execution time : 0.045 s
Press any key to continue.

#pragma warning()

#pragma warning(disable :M N;once :H;error :K)
// 表示不显示M 号和N 号的警告信息,H 号警告信息只报告一次,把K 号警告信息作为一个错误来处理。也可以将其分开来实现
//相当于:
#pragma warning(disable:M N)
#pragma warning(once:H)
#pragma warning(error:K)

猜你喜欢

转载自blog.csdn.net/qq_45279570/article/details/107750159
今日推荐