-2

Why I Can't Stop For Loop In This Program?

int main()
{
 unsigned char i;
 for(i=0;i<256;i++)
  {
   printf("%d\n",i);
  }
}
hyde
  • 60,639
  • 21
  • 115
  • 176
Karthik R
  • 7
  • 1

1 Answers1

5

Why I Can't Stop For Loop In This Program? Because variable i is declared as of unsigned char type which ranges from 0 to 255, so here

for(i=0;i<256;i++) { } /* 0,1,2..255, 0, 1 */

i never reach 256 which results in infinite loop.

From limits.h

UCHAR_MAX   255 
Achal
  • 11,821
  • 2
  • 15
  • 37