Why is the first assignment str1 [0]? Shouldn't it be str1 [1]?

In the for loop, we use ++i , which means that i incremented to 1 before the loop starts to execute. What am I misunderstood here?

Here is the code:

 #include <stdio.h> #include <string.h> int main() { char str1[100], str2[100]; int i; scanf("%s", str1); for (i = 0; str1[i] != '\0'; ++i) { str2[i] = str1[i]; } str2[i] = '\0'; printf("%s\n", str2); return 0; } 
+5
source share
4 answers

The for for syntax consists of three statements. The third (โ€œupdate stepโ€) is performed at the end of the loop body . So, in the first iteration, I am zero.

Expression

 for(init_stmt; cond_expr; update_stmt) body_stmt; 

As a result, the code will be approximately equivalent to the following:

 { init_stmt; while(cond_expr) { body_stmt; update_stmt; } } 
+5
source

I am afraid that you are mistaken: in the context of the expression for incrementing the operator for ++i , i++ , i += 1 and i = i + 1 they all have the same effect. In addition, this increment expression is executed after after each iteration before evaluating the condition, but not before the first iteration. Following are the steps to evaluate the for statement:

  • evaluates the initialization expression i = 0 ;
  • evaluate the condition expression: if it is false, exit the loop.
  • evaluate body cycle:
    • If break statement is evaluated, exit the loop
    • if the continue statement is evaluated by the branch directly in step 4;
    • otherwise go to step 4.
  • evaluate the expression of the increment ++i ;
  • go to step 2.

The last statement correctly uses str2[i] = '\0'; . The value of i at the end of the for loop is the first one that did not have a condition, for which str1[i] == '\0' , which is known as the length of the string C in str1 . This is the index where you want to keep the null delimiter in str2 .

Please note that the code can be simplified and made safer as follows:

 #include <stdio.h> int main(void) { char str1[100], str2[100]; if (scanf("%99s", str1) == 1) { for (int i = 0; (str2[i] = str1[i]) != '\0'; i++) { continue; } printf("%s\n", str2); } return 0; } 
+1
source

Your premise is incorrect. i does not increase to 1 before the start of the cycle.

 for (initialization_statement; conditional_statement; update_statement) { body_of_for_loop } 

How the for loop works, it executes the initialization statement, and then checks if the conditional statement is true . If the conditional statement is true , the body of the for loop is executed. After the body of the for loop is executed, the update instruction is executed, and then the conditional statement is calculated again, etc.

+1
source

For loop syntax:

 for (initialization; condition; increment or decrement) { //Code } 

Step 1: Initialize the variable ( i = 0 ).

Step 2 :: Check the condition ( str1[i] != '\0' )

Step 3: If the condition is True , then proceeds to the body of the loop.

Step 4: After successfully executing the loop body, it goes into the operation of increasing or decreasing the operation ( ++i ).

See image for better understanding.

img

0
source

Source: https://habr.com/ru/post/1263840/


All Articles