学习笔记 #pragma GCC diagnostic warning “-Wformat“ 的使用

本文介绍了在GCC编译器中使用#pragmaGCCdiagnosticwarning来控制-Wformat警告的开关。通过示例代码展示了如何关闭和开启特定警告,如在函数test1和test2中对-Wreturn-type警告的处理。在编译时,test2因缺少返回值而触发了-Wreturn-type警告。
摘要由CSDN通过智能技术生成

#pragma GCC diagnostic warning "-Wformat"的使用

在GCC下,#pragma GCC diagnostic warning用于表示将诊断视为警告。

格式:

#pragma GCC diagnostic warning "-Wformat"

示例程序如下:

#include <stdio.h>

/************************************************************************/
//关闭警告,诊断忽略该函数没有返回值
#pragma GCC diagnostic ignored "-Wreturn-type"

int test1(void)
{
    return;
}

//开启警告,提示该函数没有返回值
#pragma GCC diagnostic warning "-Wreturn-type"

int test2(void)
{
    return;
}

/************************************************************************/
int main(int argc, char* argv[])
{
    test1();
    test2();
    
    return 0;
}

在gcc下编译

gcc -o test test.c -Wall

函数test2会提示警告不带返回值。

test.c: 在函数‘test2’中:
test.c:17:5: 警告: 在有返回值的的函数中,‘return’不带返回值 [-Wreturn-type]

[参考资料]
GCC, the GNU Compiler Collection
GCC, Diagnostic Pragmas
#pragma GCC diagnostic ignored "-Wformat"功能介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值