I want to know the difference between ++i and i++

Asked 2 years ago, Updated 2 years ago, 86 views

What is the difference between the potential increase (++i) and the posterior increase (i++) in C? Which of the two should I use when I go around the for door?

c for-loop post-increment pre-increment

2022-09-21 18:22

2 Answers

The potential increase ++i returns the increased value after increasing the value of i by 1.

int main() {
    int i = 1;
    int j = ++i;
    printf("i:%d, j:%d", i, j);
    }

Output: i:2, j:2

Rear increase i++ returns the value before increasing after increasing the value of i by 1.

int main() {
    int i = 1;
    int j = i++;
    printf("i:%d, j:%d", i, j);
}

Output: i:2, j:1

There is a guideline to use ++i rather than i++ There's nothing set for writing "only this."


2022-09-21 18:22

stackoverflow English reference


2022-09-21 18:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.