简体   繁体   English

C 程序在运行时冻结

[英]C program freezes when run

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int x1, x2, x3, 
        y1, y2, y3, 
        ystr1, ystr2, ystr3,  
        xstr1, xstr2, xstr3, 
        sstr1, sstr2, sstr3;

    printf("Insert.");
    scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);
    ystr1 = y2 - y1;
    xstr1 = x2 - x1;
    sstr1 = sqrt(pow(xstr1, 2) + pow(ystr1, 2));
    ystr2 = y3 - y2;
    xstr2 = x3 - x2;
    sstr2 = sqrt(pow(xstr2, 2) + pow(ystr2, 2));
    ystr3 = y3 - y1;
    xstr3 = x3 - x1;
    sstr3 = sqrt(pow(xstr1, 2) + pow(ystr1, 2));
    printf("Print %d, %d, %d", sstr1, sstr2, sstr3);
    return 0;
}

For some reason this code freezes every time I enter in a single digit.出于某种原因,每次我输入一位数时,此代码都会冻结。 I really don't know what might be causing this.我真的不知道是什么导致了这种情况。 Is it the too many ints?是不是整数太多了?

The scanf function expects pointers as arguments so that it can modify the variables you're reading into. scanf函数需要指针作为参数,以便它可以修改您正在读入的变量。 Remember that all arguments in C are passed by value.请记住,C 中的所有参数都是按值传递的。 If we want to modify something with a function, we need to pass its address in memory (a pointer).如果我们想用一个函数修改某些东西,我们需要在内存中传递它的地址(一个指针)。

This is made apparent by the warnings your code generates even without explicit warnings enabled.即使没有启用显式警告,您的代码也会生成警告,这一点很明显。

test.c: In function ‘main’:
test.c:14:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
     scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);
            ~^
test.c:14:15: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
     scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);
              ~^
test.c:14:17: warning: format ‘%d’ expects argument of type ‘int *’, but argument 4 has type ‘int’ [-Wformat=]
     scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);
                ~^
test.c:14:19: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat=]
     scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);
                  ~^
test.c:14:21: warning: format ‘%d’ expects argument of type ‘int *’, but argument 6 has type ‘int’ [-Wformat=]
     scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);
                    ~^
test.c:14:23: warning: format ‘%d’ expects argument of type ‘int *’, but argument 7 has type ‘int’ [-Wformat=]
     scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);

So instead of:所以而不是:

scanf("%d%d%d%d%d%d", x1, y1, x2, y2, x3, y3);

You'd want to write:你想写:

scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3);

The & operator returns the address of a variable. &运算符返回变量的地址。

It's also important to note that scanf returns an int value.同样重要的是要注意scanf返回一个int值。 This value indicates the number of values read.该值表示读取的值的数量。 You should probably check that scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3);您可能应该检查scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3); returns 6 .返回6

if (scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3) == 6) {
   ...
}

Another thing you may wish to do is set initial values for your variables.您可能希望做的另一件事是为变量设置初始值。 If scanf fails, you'd still have that initial value.如果scanf失败,您仍然拥有该初始值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM