retagged by
3,241 views
1 votes
1 votes
void main()
{   unsigned char var=0;
    for(var=0;var<=255;var++)
    {
        printf("%d ",var);
    }

} 

What is output of this code?

retagged by

3 Answers

Best answer
5 votes
5 votes
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
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

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked Apr 16
78 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
121 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
256 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how