gpt4 book ai didi

c - 代码错误,请问是怎么回事?

转载 作者:行者123 更新时间:2023-11-30 18:38:32 24 4
gpt4 key购买 nike

我有一个问题。这段代码有什么错误?它将给出段错误:11

{
double os;
double windows = 2;

printf("Network info\n");

printf("Are you on Linux, OS X, or Windows? (1,2,3): ");
scanf("%s", os);
printf("checking..\n");
if (os == windows){
printf("Getting informatin for windows..\n");
system("ipconfig");
}else{
printf("Getting info for either osx or Linux..\n");
system("ifconfig");
}
}

最佳答案

在你的代码中,你应该改变

scanf("%s", os);

scanf("%lf", &os);

as, osdouble 类型。使用错误的格式说明符(或错误的参数类型)会调用 undefined behavior

阅读man page scanf() 的更多信息。

FWIW,对于整数值,最好使用 int 数据类型。

关于c - 代码错误,请问是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33672311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com