简体   繁体   English

解释(i = 0; 1; i ++)在C中的作用?

[英]Explanation what for(i=0;1;i++) does in C?

The required for loop starts right after fopen , what for (i=0;1;i++) exactly does? for loop所需for loop开始在fopen之后, for (i=0;1;i++)究竟是什么? when the for loop will terminate? for loop终止时? after reaching value of i=1 ? 达到i=1值后? When will that happen? 什么时候会发生? (looked for this type of loop in internet and Book C(how to program, Deitel&Deitel), without any result...) (The purpose of the code is to convert multiline final.txt to string of words[i][j]; ) (在互联网和书C中寻找这种类型的循环(如何编程,Deitel和Deitel),没有任何结果......)(代码的目的是将多行final.txt转换为words[i][j];words[i][j];

    int lines_allocated = 1000;
    int max_line_len = 150;
    double c[42][1000]={0};
    int print;

    char **words = (char **)malloc(sizeof(char*)*lines_allocated);
    if (words==NULL)
        {
        fprintf(stderr,"Out of memory (1).\n");
        exit(1);
        }

    FILE *fp = fopen("final.txt", "r");
    if (fp == NULL)
        {
        fprintf(stderr,"Error opening file.\n");
        exit(2);
        }

    int i;
    for (i=0;1;i++)
        {
        int j;

        if (i >= lines_allocated)
            {
            int new_size;

            new_size = lines_allocated*2;
            words = (char **)realloc(words,sizeof(char*)*new_size);
            if (words==NULL)
                {
                fprintf(stderr,"Out of memory.\n");
                exit(3);
                }
            lines_allocated = new_size;
            }
        words[i] = (char*)malloc(max_line_len);
        if (words[i]==NULL)
            {
            fprintf(stderr,"Out of memory (3).\n");
            exit(4);
            }
        if (fgets(words[i],max_line_len-1,fp)==NULL)
            break;

        for (j=strlen(words[i])-1;j>=0 && (words[i][j]=='\n' || words[i][j]=='\r');j--)

        words[i][j]='\0';
        }

Since in C an int can be interpreted as a boolean using a zero/non-zero rule (zero means "false", anything else means "true") the loop is going to continue until a break statement is reached inside the loop's body. 因为在C中, int可以被解释为使用零/非零规则的布尔值(零表示“假”,其他任何意思是“真”),循环将继续,直到在循环体内达到break语句。

You can rewrite the same loop as 你可以重写相同的循环

for (i=0; ;i++)

because in the absence of a condition in the middle the loop is going to continue until a break as well. 因为在中间没有条件的情况下,循环将继续直到break

what for (i=0;1;i++) exactly does? for (i=0;1;i++)到底是做什么的?

 for (i=0;1;i++)

is an infinite loop because 1 (a non zero value) is evaluates to true , hence in this case the conditional expression of for will becomes always true . 是一个无限循环,因为1 (非零值)的计算结果为true ,因此在这种情况下, for的条件表达式将始终为true

when the for loop will terminate? 当for循环终止时?

The statement 该声明

 if (fgets(words[i],max_line_len-1,fp)==NULL)
        break;  

will terminate the loop on condition being true in if . 将在条件为true时终止循环, if

after reaching value of i=1 ? 达到i=1值后?

No. I explained it above. 不,我在上面解释过。

When will that happen? 什么时候会发生?

i = 1 will happen on second iteration. i = 1将在第二次迭代时发生。 But this will not terminate the loop. 但这不会终止循环。

below snippets are exactly the same 下面的片段完全相同

for (i=0; 1; i++) {
    // some code
}


i = 0;
while (true) {
    // some code
    i++;
}

the code you written will terminated with the statement break , namely below code in your snippet 您编写的代码将以语句break结束,即在代码段中的代码下方

if (fgets(words[i],max_line_len-1,fp)==NULL)
    break; // this will jump out of the loop

That's an infinite loop! 这是一个无限循环! You should use break with some condition to come out of loop. 你应该使用具有某些条件的break来摆脱循环。

for (i=0;1;i++) is infinite loop, which equals to while(1) or while(true) in c++. for (i=0;1;i++)是无限循环,等于c ++中的while(1)或while(true)。
Since any non-zero variable will be interpreted as true for boolean variable in c. 因为对于c中的布尔变量,任何非零变量都将被解释为true。 however 然而

if (fgets(words[i],max_line_len-1,fp)==NULL)
            break;

will guarantee to exit the loop when read the end of the file. 将保证在读取文件末尾时退出循环。

while (1) or for (i=0;1;i++) is one style of loop, it must break inner the loop to make sure no infinite loop. while (1)for (i=0;1;i++)是一种循环方式,它必须打破循环内部以确保没有无限循环。 I think one advantage of this style will make the format of while or for looks uniform. 我觉得这种风格的一个优势将使格式while还是for看起来均匀。

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

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