简体   繁体   中英

++i and i++ in while loop in C

I am using a program to detect the boundary of each data type, which is like this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    /*first while loop using a++ dosesn't give us a right answer*/
    int a = 0;
    while (a++ > 0);
        printf("int max first = %d\n", a-1);

    /*second while loop using ++a performs well*/
    int b = 0;
    while (++b > 0);
        printf("int max second = %d\n", b-1);

    system("pause");
    return 0;
}

After I compile this propram and excute it, it returns:

int max first = 0
int max second = 2147483647

So I try to debug it, and I find out that in the first part, after a++ becomes 1 , then it just stop autoincrement and jump the while loop,while in second part it runs well, why is this happening?

The pre-increment operator (eg ++b ) is done first , and the value of the expression is the incremented value.

That is

int b = 0;
while (++b > 0) ...

will increment b first and then check its value using the larger-than comparison. Since in the very first iteration ++b will make b equal to 1 the condition will be 1 > 0 which is true.

Now the post -increment operator does the increment after the old value is used.

So for example a++ will return the old value of a and then do the increment.

So with

int a = 0;
while (a++ > 0) ...

the very first iteration a++ will return 0 which means you have the condition 0 > 0 which is false and the loop will never even iterate once. But the value of a will still be incremented, so afterwards it will be equal to 1 (when the loop have already ended).

This behavior of the pre- and post-operators should be part of any decent book, tutorial or class.

after a++ becomes 1, then it just stop autoincrement and jump the while loop

This happens because of the post and pre increment operators and the ; in while loop working together.

a will be incremented by 1 after the condition a++ > 0 is evaluated. Thus, the condition fails. The ; at the end of the while statement results in an empty loop and the next print statement will be executed even if the condition on which the while loop is based returns true.

This is exactly what happens in the second while loop - the pre increment operator will increment b before the condition is checked inside while (++b > 0); . The empty while loop keeps on adding one to the value of b until there is an overflow.

At this point, strictly speaking, you have invoked undefined behaviour because the operation has resulted in overflowing a signed integer.

Let me rewrite the main function you wrote - so that it becomes easier to understand.

int main()
{
    /*first while loop*/
    int a = 0;
    while (a > 0){ a = a + 1; }

    printf("int max first = %d\n", a-1);

    /*second while loop*/
    int b = 0;
    b = b + 1;
    while (b > 0){ b = b + 1; }

    printf("int max second = %d\n", b-1);

    system("pause");
    return 0;
}

Some observations regarding what happened here:

  1. Because at the beginning of the first while loop - the value of a is 0 - which is not greater than 0 ; the loop gets skipped at the beginning. As a result, the first printf outputs 0 .

  2. At the beginning of the second while loop, before evaluating the loop control condition; the loop control variable b gets incremented by 1 , resulting the value of b becoming 1 ; which is greater than 0 . For this reason, the second loop is executed.

  3. While executing the second loop, the value of b keeps incrementing by 1 until the value of b overflows. At this point, the program encounters undefined behaviour - and exits the while loop if the program doesn't crash or keeps executing the loop indefinitely (in which case, at some stage the OS will terminate the program; or ask the user to terminate it - as the program will become non-responsive).

You mentioned that you wanted to measure the limit of int values; I hope this reference and this reference will help you in some way.

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