C程序陷入无限循环样态,但没有打印任何变量

时间:2018-06-28 18:33:34

标签: c gcc

我正在尝试K&R练习1-29,说实话,我对我的程序正在做的事情感到困惑。用gcc输入一个小的文本文件后,该程序将永久运行,就好像存在无限循环一样。再次通读循环,看不到任何明显的错误,因此我尝试打印不同的变量以查看正在发生的情况,但是命令行上却没有输出任何内容-甚至从第一个for循环的变量i,甚至一次都没有。这是我的代码:

#include <stdio.h>
#define tabStop 10
#define maxInput 1000

int main(){

int i, c, b;
int m, t = 0;
int index, index2;
char input[maxInput];
int nonBlank;

for (i = 0; i<maxInput-1 && (c=getchar()) != EOF; ++i){
    if (c != ' '){
        input[i] = c;
    }
    else {
        index = i; /*stores location in character array at start of blanks*/
        for (b = 0; c == ' '; ++b, ++i); /*counts the number of blanks (b) until the next non-blank character*/
        nonBlank = input[i]; /*saves the first seen non-blank character to be re-added later (as getchar() will have taken it in from input and will now stay at that point of the input)*/
        index2 = i; /*stores location in character array at end of blanks*/
        if (b >= tabStop){ /*if the tab space fits inside the number of blanks, otherwise there is nothing to be done*/
            while (t < b){
                t += tabStop;
                ++m;
                }
        for (int x = 0, i = index; i != index2 && x <= m; ++i, ++x){ /*loops over the number of tabs to be added starting from the first blank in the array found up until the first non-blank*/
                input[i] = '\t';
                }
            while (i != index2){ /*if i did not reach index2 before x surpassed m, there exist remaining spaces to be filled with blanks*/
                input[i] = ' ';
                ++i;
                }
            }
            input[i] = nonBlank; /*puts the first seen non-blank character into place, as getchar() has already covered it and so it wouldn't otherwise be added like other non-blanks*/
        }
    }

input[i] = '\0';

printf("%s", input);

return 0;

}

这是输入的文本文件:

hello           there       world

0 个答案:

没有答案