#include <stdio.h>
int main() {
int t;
scanf("%d", &t);
printf("%d", t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我使用ideone.com编译了上面的C代码,并弹出以下警告:
prog.c:在函数'main'中:
prog.c:5:警告:忽略'scanf'的返回值,使用属性warn_unused_result声明
有人能帮我理解这个警告吗?
我只有几天的C编程,所以我不太确定这段代码有什么问题:
#include <stdio.h>
int main(int argc, char * argv[]) {
int sides;
printf("Please enter length of three sides :\n");
scanf("%d", &sides);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息如下:
忽略scanf的返回值
我在这里做错了什么,我该怎么做才能解决它?
对于嵌入式系统中的外围设备要求,我必须声明一个变量以读取寄存器,但是以后将不再使用该值。因此,我自然会得到关于未使用变量的编译器警告。如何抑制警告?我有两种想法:
添加从变量读取的虚拟数据,例如:
volatile int var;
var = peripheral_register;
var = var;
Run Code Online (Sandbox Code Playgroud)你有更好的主意吗?
我最近了解到printf()返回它要调用的字符数.但我想知道我之前从未"使用过"这个返回值,例如:
int c = printf("Called func\n");
Run Code Online (Sandbox Code Playgroud)
要么
printf("%d", printf("Called func\n"));
Run Code Online (Sandbox Code Playgroud)
我只是写了一些语句,其中执行了printf的功能,如:
printf("Called func\n");
Run Code Online (Sandbox Code Playgroud)
那么printf()的返回值"发生了什么"?编译器如何知道它"ok"不使用返回的整数?