#include <stdio.h>
#define MAX 255
int main()
{
unsigned char a[MAX],i;
for (i=0;i<=MAX;i++)
{
a[i]=i;
}
for (i=0;i<=MAX;i++)
{
printf("%d ",a[i]);
}
#define MAX 255
int main()
{
unsigned char a[MAX],i;
for (i=0;i<=MAX;i++)
{
a[i]=i;
}
for (i=0;i<=MAX;i++)
{
printf("%d ",a[i]);
}
}
//分析
unsigned char 类型的数据的取值范围在0-255,有如果其值为255,如果再加1则值为0.所以上述的示例会产生两个问题:1.数组的越界,超过了数组最多容纳的255个字符。2.在第一个for循环中会出现死循环,i=255时,i++为0.
下面是正确的代码,请参考
#include <stdio.h>
#define MAX 255
int main()
{
unsigned char a[MAX],i;
for (i=0;i<MAX;i++)
{
a[i]=i;
}
for (i=0;i<MAX;i++)
{
printf("%d ",a[i]);
}
}