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
- Add two unsigned numbers using bits
- for_each loop in C++
- Print 1 to 100 in C++, without loop and recursion
- Difference between while and do-while loop in C, C++, Java
- Range-based for loop in C++
- How will you print numbers from 1 to 100 without using loop? | Set-2
- How will you print numbers from 1 to 100 without using loop?
- A nested loop puzzle
- Print pattern using only one loop | Set 1 (Using setw)
- How to concatenate two integer arrays without using loop in C ?
- How to print a number 100 times without using loop and recursion in C?
- Print a character n times without using loop, recursion or goto in C++
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Why are elementwise additions much faster in separate loops than in a combined loop?
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?