打印变量绑定到另一个打印

时间:2016-01-25 00:59:45

标签: c

我在程序结束时打印了四个变量。

    int n = 0;
    for (n = 0; n < count; n++)
        printf("sorted array:%d\n ", array[n]);

        int last = array[0];
        int unique = 1;
        int i;
        for (i = 1; i < n; i++) {
            if (array[i] != last) {
                last = array[i];
                unique++;
            }
        }

        char *start;
        int c;
        int value;
        int step;

        c = 0;
        start = line;
        while (sscanf(start, "%d%n", array + c, &step) == 1) {
            start += step;
            c += 1;
        }

        value = 1;
        int j;
        for (j = 1; j < c; ++j) {
            value += (array[j] - array[j - 1]) ? 1 : 0;
        }

        printf("integers: %d ", count);

    }

    /* Close the file */
    fclose(fp);
    return 0;
}

我可以打印变量unique的正确值的唯一方法是保留printf("sorted array:%d\n", array[n]);。但是,我使用printf语句仅用于测试目的,并且不希望在我的程序执行时打印它。

如果我删除了for(n = 0; n < count; n++)printf("sorted array:%d\n", array[n]);,则unique的值始终是不正确的。

我以前从未见过这样的事情。有关为什么一个变量的printf似乎与另一个变量绑定的任何建议?

1 个答案:

答案 0 :(得分:5)

如果你保持for循环,那么在最后,我们有n = count。如果删除它,则为n = 0。然后,这会更改您的第二个for循环,因为i不会采用相同的值。