代码错误,怎么了?
Error in code, What's wrong?
我有一个问题。这段代码有什么错误?它会给出一个分段错误: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);
因为,os
是double
类型。使用错误的格式说明符(或错误的参数类型)调用 undefined behavior。
阅读 scanf()
的 man page 了解更多信息。
FWIW,对于整数值,最好使用 int
数据类型。
我有一个问题。这段代码有什么错误?它会给出一个分段错误: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);
因为,os
是double
类型。使用错误的格式说明符(或错误的参数类型)调用 undefined behavior。
阅读 scanf()
的 man page 了解更多信息。
FWIW,对于整数值,最好使用 int
数据类型。