简体   繁体   English

C中while循环中的++i和i++

[英]++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:在我编译这个 propram 并执行它之后,它返回:

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?于是我调试了一下,发现在第一部分, a++变为1 ,它只是停止自增并跳转while循环,而在第二部分它运行良好,为什么会发生这种情况?

The pre-increment operator (eg ++b ) is done first , and the value of the expression is the incremented value.预增量操作者(例如++b首先进行,和表达的值是增加后的值。

That is那是

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

will increment b first and then check its value using the larger-than comparison.首先增加b ,然后使用大于比较检查其值。 Since in the very first iteration ++b will make b equal to 1 the condition will be 1 > 0 which is true.由于在第一次迭代++b将使b等于1因此条件将是1 > 0 ,这是真的。

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.因此,例如, a++将返回a然后做增量。

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.第一次迭代a++将返回0 ,这意味着您的条件0 > 0,并且循环甚至永远不会迭代一次。 But the value of a will still be incremented, so afterwards it will be equal to 1 (when the loop have already ended).但价值a仍然会增加,所以后来就等于1 (当循环已经结束)。

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在 a++ 变为 1 后,它只是停止自动递增并跳转 while 循环

This happens because of the post and pre increment operators and the ;发生这种情况是因为 post 和 pre 增量运算符以及; in while loop working together.while循环中一起工作。

a will be incremented by 1 after the condition a++ > 0 is evaluated. a评估条件a++ > 0a将增加1 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.while语句的末尾会导致一个空循环,即使while循环所基于的条件返回 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);这正是第二个while循环中发生的情况 - 在while (++b > 0);内部检查条件之前,预增量运算符将增加b while (++b > 0); . . The empty while loop keeps on adding one to the value of b until there is an overflow.空的while循环不断地给b的值加 1,直到出现溢出。

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.让我重写您编写的main函数 - 使其更容易理解。

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 ;因为在第一个 while 循环开始时 - a值为0 - 不大于0 the loop gets skipped at the beginning.循环在开始时被跳过。 As a result, the first printf outputs 0 .结果,第一个printf输出0

  2. At the beginning of the second while loop, before evaluating the loop control condition;在第二个while循环开始时,在评估循环控制条件之前; the loop control variable b gets incremented by 1 , resulting the value of b becoming 1 ;循环控制变量b增加1 ,导致b的值变为1 which is greater than 0 .大于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.在执行第二个循环时, b的值一直递增1直到b的值溢出。 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).此时,程序遇到未定义的行为- 如果程序没有崩溃或无限期地继续执行循环(在这种情况下,操作系统将在某个阶段终止程序;或要求用户终止它),则退出while循环- 因为程序将变得无响应)。

You mentioned that you wanted to measure the limit of int values;您提到要测量int值的限制; I hope this reference and this reference will help you in some way.我希望这个参考这个参考能在某种程度上帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM