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

} 

What is output of this code?

3 Answers

Best answer
5 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 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 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
Position:
Show:

Related questions

1 1 vote
1 1 answer
127
127 views
khanshahima4 asked 4 days ago
127 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
13 13 votes
1 1 answer
1.2k
1.2k views
GO Classes asked Jul 2
1,230 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
16 16 votes
2 2 answers
660
660 views
GO Classes asked Jul 2
660 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
9 9 votes
1 1 answer
490
490 views
GO Classes asked Jul 2
490 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...