0

The output of below c program is, output : 1,2,3,4 ........ 126,127,-128,-127 .... -2,-1 ?

#include <stdio.h>
#include <string.h>

int main()
{
   char i=0;
   for(i<=5 && i>=-1 ; ++i;i>0)
     printf("%d\n",i);
   printf("\n");
   return 0;

}

please explain why is so ?

dcoder11
  • 31
  • 4

5 Answers5

0

It is called overflow. Type "char" uses 1 byte of your RAM's capacity, so it can store only 256 values. These are [-128, 127]. When you try to get over 127, it will get back the the lowest value.

Other than that, your for loop is a little messed up. Try

for ( i = 0 ; i <= 5 ; ++i ) // This will print 0,1,2,3,4,5
    printf( "%d\n" , i );
Giwrgos Tsopanoglou
  • 1,165
  • 2
  • 8
  • 15
0

Because this:

for(i<=5 && i>=-1 ; ++i;i>0)

works out as

1) Initialisation:

   i<=5 && i>=-1

does nothing

2) Termination condition:

   ++i

increments i and will terminate when i gets to zero

3) statement executed each time round loop (normally an increment / decrement):

   i > 0

does nothing.

So your code loops from i = 0 back till it's zero again

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
0

I'm guessing you're using the for loop incorrectly.

for(i<=5 && i>=-1 ; ++i;i>0)

The above code means:

  1. Before anything, evaluate i<=5 && i>=-1 (which doesn't have any side effects so nothing happens)
  2. On every iteration, increment i then check if it is zero. If it is non-zero, run another iteration.
  3. At the end of every iteration, evaluate i>0 (which again, does nothing).

Therefore, your loop simplifies down to and is essentially

while (++i)

To explain your result, the system you're using is likely using implements a char as a signed two's complement integer which 'wraps' to a negative number when it is higher than 127 (since 128 in a 8 bit two's complement integer is a -128)

tangrs
  • 9,709
  • 1
  • 38
  • 53
0

First of all, char occupies 1 byte and it is signed. With the help of 8 bits (1 byte) the range of numbers that you can represent is

**-(2^(8-1)) to +((2^(8-1)) -1) [ie from -128 to +127].**

In your code you are incrementing i and also printing the same. once i reaches 127 (in binary 0111 1111 = 127) and you again increment it becomes (1000 0000) which is -128.

And while printing you are using %d. so out will be in integer format. That is why it is printing 1,2,3 ... -128, -127...

If you didn't understand how 1000 0000 is -128, read

What is 2's Complement Number?

Community
  • 1
  • 1
0

Here is a breakdown of your code, it's fairly sneaky:

for( i <= 5 && i >= -1 ; ++i; i > 0)

Typically a for-loop is, ( initial statement, expression, second statement ). Looking at your code before the first null statement: what you have done is an expression (in place of a statement), which does not matter at all however it's entirely useless. So removing that line (which has no effect on the result of this expression) gives us:

for( ; ++i; i > 0)

... now if you noticed you initialized i to be 0 before the for loop. What you are doing next is incrementing i and then returning its value (see here), and hence it will go from 1 ... -1 (overflowing at 127). This is because in C any non-zero value is true and 0 is false. Hence, once i becomes 0 it will stop running the loop. i can only become zero by overflowing.

Your third statement does not matter, it's irrelevant.

Jacob Pollack
  • 3,703
  • 1
  • 17
  • 39