-4

I know the i++ means return i and then increase 1, and ++i means increase 1 then return i. In this code:

int isTotalAlphabetical(char word[]){
int i = 0;
while (word[i] != '\0') {
    printf("The %dth is: %d, the char is %c\n", i,isalpha(word[i]), word[i]);
    if (!isalpha(word[i])){
        printf("now return 0, because the %c is not alpha, it's the %d char\n", word[i], i);
        return 0;
    }
    i++;

}
return 1;

}

In this loop, when I move i++ to "while (word[i++] != '\0')" the output will print out info that

The 2th is: 1, the char is p
The 3th is: 1, the char is l
The 4th is: 1, the char is e
The 5th is: 0, the char is 
now return 0, because the  is not alpha, it's the 5 char

When I move ++i to "while (word[++i] != '\0')", the output will ignore the first one char. it shows:

The 1th is: 1, the char is p
The 2th is: 1, the char is p
The 3th is: 1, the char is l
The 4th is: 1, the char is e

How could that happen?

Shawn Jin
  • 19
  • 1
  • 4
  • 2
    Your own first sentence (*I know...*) answers your question. – PM 77-1 Sep 13 '18 at 17:24
  • 1
    Why would you expect the results *not* to differ? For a given initial value of `i`, the expressions `++i` and `i++` evaluate to different values. If that were not the case, it would be redundant to have both. – John Bollinger Sep 13 '18 at 17:24
  • 1
    @user2864740, the redundancy comment was a *post hoc* observation, not an argument. Also, although I accept that there is no promise of non-redundancy, I don't think the commutativity of the `[]` operator (or of the `+` operator) demonstrates a redundant language feature. – John Bollinger Sep 13 '18 at 19:29

1 Answers1

1

Ex1:

int x=1; int y = 2 + (x++);

The value of y is 3 because x is assinged to 1. After caculating the value of y, the value of x is increased 1.

Ex2:

int y = 2 +(++x); The value of y is 4 because the value of x is increased 1 unit before caculating y’s value.

I’m try to help you out but English is not my mother language.