Error while executing code MSB6006 "CL.exeexecuted

2019-08-23 01:46发布

I am getting the following error while executing code on Visual Studio 2019 MSB6006"CL.exe" exited with code 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条回答
Anthone
2楼-- · 2019-08-23 02:29

Your order of statements is off.
First assign values to a, b, and c.
Only after use those values in calculations.

#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;
}

Note: the return value of scanf() should be checked to be sure all of a, b, and c have valid values.

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

Note 2: I changed your code a little bit: removed non-standard <conio.h>, changed the placing of most '\n' to be more line-oriented, replaced the optional scanf_s (this function may not exist in all C11/C18 implementations).

查看更多
登录 后发表回答