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

4

5

In the for loop, we are using ++i which means that i is incremented to 1 before the loop starts to execute. What am I getting wrong 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;
}
Interwork answered 5/2, 2017 at 13:40 Comment(4)
"using ++i which means that i is incremented to 1 before the loop starts" no; what makes you assuming this?Shammer
I don't understand why to downvote this question, it's one of the few questions that makes sense and is very valid.Febrifuge
For your reference the related chapter from the C-Standard (draft): "for ( clause-1 ; expression-2 ; expression-3 ) statement [...] The expression expression-3 is evaluated as a void expression after each execution of the loop body."Shammer
This is because of the flow of execution of the for loop, ++i is appeared first in the code but reserved for later execution after the body of the loop,when execution control comes to ++i then i is incremented first.however it doesn't matter you preincrement or postincrement in such places where incremention part is not related to any other expression,that is nothing happening between the evaluation of i and incremention of i, as here i++>3 i is compared first with 3 then incremented but in ++i i is incremented first then compared.Lichenin
A
5

The for loop syntax is composed of three statements. The third ("update step") is executed at the end of the loop body. So in the first iteration, i is zero.

The expression

for(init_stmt; cond_expr; update_stmt)
  body_stmt;

Will result in code roughly equivalent to the following:

{
  init_stmt;
  while(cond_expr) {
    body_stmt;
    update_stmt;
  }
}
Aftercare answered 5/2, 2017 at 13:41 Comment(2)
We can call it an update statement rather than an inc_stmt.Fimbriation
@Fimbriation - That is indeed a better term for it, thank you.Aftercare
C
1

I'm afraid you are mistaken: in the context of the increment expression of the for statement, ++i, i++, i += 1 and i = i + 1 all have the same effect. Furthermore, this increment expression is executed once after each iteration, before evaluating the condition, but not before the first iteration. Here are the steps for the for statement evaluation:

  1. evaluates the initialization expression i = 0 ;
  2. evaluate the condition expression: if it is false, exit from the loop.
  3. evaluate the body of the loop:
    • if a break statement is evaluated, exit the loop
    • if a continue statement is evaluated branch directly to step 4 ;
    • otherwise, branch to step 4.
  4. evaluate the increment expression ++i ;
  5. branch 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 that failed the condition, the one for which str1[i] == '\0', which is known as the length of the C string in str1. This is the index at which you want to store the null terminator in str2.

Note that the code can be simplified and made safer this way:

#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;
}
Copley answered 5/2, 2017 at 13:55 Comment(0)
F
1

Your premise is wrong. i is not incremented to 1 before the loop starts.

for (initialization_statement; conditional_statement; update_statement) {
    body_of_for_loop
}

The way for loop works is 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. Once the body of the for loop is executed, the update statement is executed and then again the conditional statement is evaluated and so on.

Fimbriation answered 5/2, 2017 at 14:4 Comment(0)
W
0

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 condition True, then goes to for loop body.

Step 4: After successful execution of loop’s body, goes to increment or decrement operation part(++i).

See image for more understanding.

img

Wellmannered answered 5/2, 2017 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.