执行代码MSB6006“ CL.exe用代码2执行时出错

时间:2019-07-20 09:53:56

标签: c visual-studio

在Visual Studio 2019上执行代码时出现以下错误 MSB6006“ CL.exe”以代码2退出

#include<stdio.h>
#include<conio.h>

int main()
{
int a, b, c,x;
x = a / (b - c);

printf("\n Enter values of a,b and c");
scanf_s("%d%d%d", &a, &b, &c);
printf("\n The value of x is %d", x);
return 0;

}

1 个答案:

答案 0 :(得分:1)

您的语句顺序已关闭。
首先将值分配给abc
只有在计算中使用了这些值之后。

#include <stdio.h>

int main(void) {
    int a, b, c, x;
    // x = a / (b - c); // NOPE! a, b, and c have no valid values

    printf("Enter values of a, b and c\n");
    scanf("%d%d%d", &a, &b, &c);
    x = a / (b - c);    // calculation moved here; a, b, and c (hopefully) have valid values now
    printf("The value of x is %d\n", x);
    return 0;
}

注意:应检查scanf()的返回值,以确保abc的所有值都有效。

if (scanf("%d%d%d", &a, &b, &c) != 3) /* error */;

注2:我做了一些更改:删除了非标准的<conio.h>,将大多数'\n'的放置改为面向行,替换了可选的{{1 }}(此功能可能并非在所有C11 / C18实现中都存在)。