retagged by
3,199 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
SSR17 asked Feb 29
207 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
2 votes
2 votes
1 answer
2
rupamsardar asked Aug 30, 2023
468 views
#include <stdio.h int f(int x) { if(x%2==0) { return f(f(x-1)); } else return (x++); } int main() { printf("%d",f(12)); ret...
5 votes
5 votes
2 answers
3
saurabh0709 asked Aug 1, 2023
1,136 views
What will be the output of the following code? _______ #include <stdio.h int main(){ char val=250; int ans; ans= val+ !val + ~val + ++val; printf("%d", ans); return 0; }
2 votes
2 votes
1 answer
4