Help.....!!
Please explain me the output of the following code.I am unable to understand why infinte loop is occuring for i>126.

#include<stdio.h>
int main(void)
{
   char i=2;
   while(i<=127)
   {
      printf("%d\n",i);
      i+=1;
   }
   return 0;
}

Here's a question. How big can a character variable get?

Basically range= -2^(n-1) to 2 ^(n-1)-1.
where n=number of bits.

for char n=8.
so range = -2^7 to 2^7-1
i.e., -128 to 127

please help me...Yet I ve not found the answer any where , I am very tensed from this question

please help me...Yet I ve not found the answer any where , I am very tensed from this question

A character variable largest integer value is 127. Once it reaches 127(the character variable) the value will wrap around to the lowest values which is -128. So do you see? A character variable can't be larger than 127.

What does your program print before and after it goes wrong?

i ve not any problems with what output are coming but I just want to know why infinite loop is occuring.

As Gerard said earlier ,

Once it reaches 127(the character variable) the value will wrap around to the lowest values which is -128.

so this will happen with your code.
when i becomes127 (for the first time);

while(i<=127)

since it satisfies the loop.
will go inside and prints the value of i(=127).

printf("%d\n",i);

then increases the i by 1

i+=1;

.
but since the character value can not be greater than 127, it wraps around to the lowest value which is -128 in case of char.
hence i=-127
Now again the loop will check the condition

while(i<=127)

since it satisfies the loop condition(-128 is smaller than 128)
it will go in and prints the value of i.
and again add 1 to it.consequently i=-127.
In this way the value of i cycles from 1 to 127 becomes -128
again from -128 to 127 becomes -128 and so on.
hence the loop will never terminate.

thanks to all...i got it

A character variable largest integer value is 127.

Assuming CHAR_BIT is 8 and vanilla char is signed.

Once it reaches 127(the character variable) the value will wrap around to the lowest values which is -128.

That's an implementation detail. The C standard says that overflowing a signed quantity will invoke undefined behavior. Only unsigned types have well-defined wraparound semantics. However, that's not to say that under or overflow on signed types doesn't often result in wraparound, which explains the OP's behavior, it's just not required behavior.

A character variable can't be larger than 127.

It's correct to say that the range of char cannot be smaller than [-127,127], but it can be larger. This is an implementation detail, and the CHAR_BIT macro in <limits.h> will tell you how many bits there are in a byte on the implementation.

Further, char may be either signed or unsigned. If it's signed (assuming a CHAR_BIT of 8), the safe range is [-127,127]. If it's unsigned, the safe range is [0,255]. Note that for the common two's complement representation, the most negative number is one greater: [-128,127]. This is a quirk of the representation and shouldn't be assumed, which is why I spoke of the "safe" range. The safe range is the required standard range for a given bit size, it's the intersection of the known binary representation schemes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.