简体   繁体   中英

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? when the for loop will terminate? after reaching value of 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]; )

    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.

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.

what for (i=0;1;i++) exactly does?

 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 .

when the for loop will terminate?

The statement

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

will terminate the loop on condition being true in if .

after reaching value of i=1 ?

No. I explained it above.

When will that happen?

i = 1 will happen on second iteration. 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

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.

for (i=0;1;i++) is infinite loop, which equals to while(1) or while(true) in c++.
Since any non-zero variable will be interpreted as true for boolean variable in c. 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. I think one advantage of this style will make the format of while or for looks uniform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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