Let us have a look at following code snippet in C/C++.
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:
Output on GCC (Undefined Behavior in Standard):
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.
Output: Numbers from 0 to 255
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.
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
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 email@example.com. 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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum value of unsigned long long int in C++
- What happens if we mix new and free in C++?
- Add two unsigned numbers using bits
- What happens when a function is called before its declaration in C?
- What happens when more restrictive access is given to a derived class method in C++?
- What happens when a virtual function is called inside a non-virtual function in C++
- what happens when you don't free memory after using malloc()
- Print 1 to 100 in C++, without loop and recursion
- Print substring of a given string without using any string function and loop in C
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- How to print a number 100 times without using loop and recursion in C?
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Difference between while and do-while loop in C, C++, Java
- Difference between Sentinel and Counter Controlled Loop in C
- How to find length of a string without string.h and loop in C?
- How will you print numbers from 1 to 100 without using loop?
- A nested loop puzzle
- How to concatenate two integer arrays without using loop in C ?
- Print pattern using only one loop | Set 1 (Using setw)
- for_each loop in C++
Improved By : SHUBHAMSINGH10