Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22027

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

$
0
0

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?


Viewing all articles
Browse latest Browse all 22027

Trending Articles