in Programming in C retagged by
3,215 views
1 vote
1 vote
void main()
{   unsigned char var=0;
    for(var=0;var<=255;var++)
    {
        printf("%d ",var);
    }

} 

What is output of this code?

in Programming in C retagged by
by
3.2k views

4 Comments

It prints 0 1 2.....255.
0
0
no its a infinite loop :D
0
0
Why it go into infinite loop?
0
0
answer added
0
0

3 Answers

5 votes
5 votes
Best answer
Infinite loop 0,1,......255,0,1,................................................
Reason is you are declaring var   as a unsigned char which gets 1B = 8 bits of memory  and max unsigned value possible is 255
Rangle 0 to 255
whenver var = 255 = 1111 1111   then var + 1  is  
1111 1111
           +1
________
0000 0000      carry flag = 1
________
value is wrapped around :D
selected by
by
0 votes
0 votes
The range of unsigned char is 255.so it will start from 0 to 255,then again starts from 0 and so on and will get stuck into infinite loop.
0 votes
0 votes
infinite loop as range of char  in signed -128 to +127 but in unsigned

it will be 0 to 255 which means according to this question no value in between this no.s will be less then 255

hence infinite loop

Related questions