c语言prog.c: in function 'main':,c - Warning: ignoring return value of 'scanf', declared with attribut...

After reading all answers and comments on this page I don't see these yet another options to avoid the warning:

When compiling with gcc you can add to your command line:

gcc -Wall -Wextra -Wno-unused-result proc.c -o prog.x

Another option is to use -O0 as "optimization level zero" ignores the warning.

Using cast to (void) is simply useless when compiling with gcc

If debugging your code, you can always use assert() as in the example bellow:

u = scanf("%d", &t);

assert(u == 1);

But now, if you turn off assert via #define NDEBUG you will get a -Wunused-but-set-variable. You can then turn off this second warning by one of two ways:

Adding -Wno-unused-but-set-variable to your gcc command line, or

Declaring the variable with attribute: int u __attribute__((unused));

As pointed out in other answer, the second option unfortunately is not very portable, although it seems the best option.

At last, the defined MACRO bellow can help you if you are sure you want to ignore the return of a given function, but you are not comfortable turning off the warnings for all unused returns of functions:

#define igr(x) {__typeof__(x) __attribute__((unused)) d=(x);}

double __attribute__ ((warn_unused_result)) fa(void) {return 2.2;}

igr(fa());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值