Skip to content
Related Articles

Related Articles

What happens if loop till Maximum of Signed and Unsigned in C/C++?
  • Difficulty Level : Easy
  • Last Updated : 31 Mar, 2020

Let us have a look at following code snippet in C/C++.

C++




// An Unsigned char example
#include <iostream>
using namespace std;
void fun1()
{
    unsigned char i;
    for (i = 0; i < 256; i++)
        cout << i << " ";
}
  
int main()
{
    fun1();
    return 0;
}
  
// This code is contributed by shubhamsingh10


C




// An Unsigned char example
#include<stdio.h>
void fun1()
{
    unsigned char i;
    for (i = 0; i < 256; i++)
        printf("%d ", i);
}
  
int main()
{
    fun1();
    return 0;
}



Output:

 Infinite Loop 

Explanation:

We know that the size of the character variable is 8 bits or 1 byte. Hence by base 2 representation of decimal numbers, the maximum number in 8 bits is 11111111.This is because the range of unsigned numbers in 8 bits ranges from 0 to 28-1

Now (11111111)2 = (255)10



If we drive the loop upto 255 starting from 0, it will execute the statement in the loop 256 times (both 0 and 255 inclusive). As the loop reaches to (255)10, After executing it, the variable “i” is incremented by 1 i.e. going by the 2s complement arithmetic,

(11111111)2 + (00000001)2 = (00000000)10

Note: the end carry is discarded in this case; hence the final incremented number is 0, which results in re-execution of the loop, hence the loop runs for the infinite times. Therefore the above condition can be avoided if we put limit to unsigned char i to be lesser than 255 rather than 256.

Now consider below program:

C++




// A signed char example
#include <iostream>
using namespace std;
  
void fun2()
{
    signed char i;
    for (i = 0; i < 128; i++)
        cout << i <<" ";
}
  
int main()
{
    fun2();
    return 0;
}
  
// This code is contributed by shubhamsingh10


C




// A signed char example
#include<stdio.h>
  
void fun2()
{
    signed char i;
    for (i=0; i<128; i++)
        printf("%d ",i);
}
  
int main()
{
    fun2();
    return 0;
}



Output on GCC (Undefined Behavior in Standard):

 Infinite Loop  

Signed char range belongs from -27 to 27-1, hence it also goes for the infinite execution if the limit is <128.

Note that the 2s complement of (127)10 is (01111111)2 adding 1 to which will give us (10000000)2, which is –(128)10 when calculated from 2s complement form.



So how to loop from 0 to max (255 or 128 or any other max limit)?
One way of doing this is below.




// One way of looping till maximum of unsigned in C/C++
#include<stdio.h>
void fun1()
{
    unsigned char i = 0;
    do
    {
        printf("%d ", i);
        i++;
    }
    while (i > 0);
}
int main()
{
    fun1();
    return 0;
}


Output: Numbers from 0 to 255




// One way of looping till maximum of signed in C/C++
// (Same as above except first statement)
#include<stdio.h>
void fun2()
{
    signed char i = 0;
    do
    {
        printf("%d ", i);
        i++;
    }
    while (i > 0);
}
int main()
{
    fun2();
    return 0;
}


Output in GCC: Numbers from 0 to 127


Note: In C, signed overflow is undefined behavior, hence the above solution may not work on all machines for signed numbers. Also, the output shown above for signed may not be same on all machines. The behavior is well defined for unsigned numbers.

Following are mentioned in C99 here.

About Unsigned:
A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type

About Signed:
An example of undefined behavior is the behavior on integer overflow.

This article is contributed by Pranjal Mathur. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :