Hang c language --for infinite loop

Let us look at the following two loop, I had just stepped on the hole, the second hole, and therefore specially recorded it. char are unsigned, so pay special attention and in keil iar in.

I char unsigned;
for (I = 0; I <256; I ++)
{
// something
}
        When we want to achieve the above code using a small loop, the result did not, in fact, this is another cycle of death kinds of writing, because only the largest unsigned variable i is 255, worse, the compiler does not point out the error.

With a similar code is:

i char unsigned;
for (i = 10; i> = 0; i--)
{
// something
}
This is an endless loop, what is the reason you see it? No matter how Save i, i is greater than or equal to 0.

        This tells us that the range for each variable of the type to be a clear understanding. It is noteworthy that the same variable type will be different for different CPU architecture and compiler different results. In most such int type uses 16-bit CPU architecture in two bytes, 32-bit CPU, but often 4 bytes; char type in most compilers are signed numbers, but keil MDK in it is unsigned, if you want to define in keil MDK signed char type variable must be explicitly declared with signed. I once read a book in which there is a saying: "signed keyword is very generous, you can also completely when it does not exist in the default state, the compiler default data bit signed types", this sentence is disagrees, we should be master of his own use of the CPU architecture and compiler.

Original link: https: //blog.csdn.net/zhzht19861011/article/details/7251644

Guess you like

Origin www.cnblogs.com/CodeWorkerLiMing/p/12146085.html