The Size of Unsigned Integers in C++:
The size of a typical unsigned integer in C++ is 4 bytes. To confirm this, you can take a look at the following code snippet:
In this code snippet, we have just used the “cout” statement with the “sizeof” function. The parameter that we implemented to this function is “unsigned int”. It means that this statement will return the size of an unsigned integer in bytes.
After executing this C++ code snippet, we received the output shown in the following image, which confirms that the unsigned integer in C++ has the size of 4 bytes:
How to Declare an Unsigned Integer in C++?
Unsigned integers can be declared very easily in C++. You can see the following code snippet declare an unsigned integer in C++ in Ubuntu 20.04:
In this code snippet, we have declared an unsigned integer with the statement “unsigned int var” and assigned to it the value “255”. Then, we have used the “cout” statement to display the value of this unsigned integer on the terminal.
In Which Scenarios Should We Avoid Using the Unsigned Integers in C++?
There are certain situations in which the usage of the unsigned integers should be strictly avoided in C++ because by doing so, it can lead to some highly erroneous outputs. These errors cannot be detected simply by looking at the C++ code. Moreover, they also cannot be detected at the compile-time. Instead, you will only be able to catch those errors once you execute your C++ code. Let us take a look at some of these scenarios so that you can avoid using the unsigned integers in C++ in these cases.
Scenario # 1: Assigning a Negative Number to an Unsigned Integer Variable:
The unsigned integers are used to hold only the non-negative whole numbers. However, the programmer can still assign a negative number to an unsigned integer variable. This negative number is always wrapped around to the nearest number within the range. This concept is known as modulo wrapping. No error is generated in this case, but the value you get is definitely not the correct one. You can review the following code snippet to understand this:
In this code snippet, we have assigned the value “-1” to an unsigned integer variable. Then, to check whether the correct value is printed on the terminal or not, we have used the “cout” statement with the variable “var”.
Upon executing this code, we got to know that the value of the unsigned integer variable became “4294967295” instead of “-1”, as shown in the image below:
Scenario # 2: Performing Subtraction of Two Unsigned Integers in Which the Number to be Subtracted Is Larger Than the Number to be Subtracted From:
Whenever we perform a subtraction of two integers in a situation where the first number is smaller than the second, then we always receive a negative output. Therefore, in this situation, we should never make use of the unsigned integers. The following code snippet demonstrates this:
In this code snippet, we have two unsigned integers, “var1” and “var2”, whose values are “2” and “7”, respectively. We wanted to subtract “2” from “7”. The result of this subtraction is “-5”. However, you will have to execute this code snippet to figure out how this subtraction works with unsigned integers.
The execution of this code snippet produced the result shown in the following image. Again, although we did not receive any error message, our output still turned out to be incorrect.
Scenario # 3: Using the Unsigned Integers With Conditional Statements:
There are some conditions that execute when one variable is less than another. However, if we compare the negative and positive numbers while using the unsigned integers, then the result might be unexpected. To elaborate this, we have designed the following code snippet:
In this code snippet, we have two unsigned integer variables, “var1” and “var2” whose values are “-1” and “1” respectively. Then, we have an “if” statement that will be executed if “var1<var2”, which is logically true. However, since we have used the unsigned integers, then “var1” will be wrapped around to the nearest unsigned integer and will become larger than the value of “var2”. Due to this, the “else” statement will be executed.
To witness this, you can see the output shown in the following image. This output states “var1” is not less than “var2”, which is logically and mathematically incorrect:
Scenario # 4: Calling Functions With Unsigned Integers:
At times, you define such functions that accept unsigned integers as parameters. Again, if someone passes a negative value to this function, it will still be called but will produce wrong results. To depict this, we have implemented the following code snippet:
In this code snippet, we have a function named “DisplayNumber”, which accepts an unsigned integer “num”. Within this function, we simply want to print the value of this number on the terminal. In our “main()” function, we have called this function with the number “-1”.
You can see the output of this code snippet in the image shown below. According to this output, the value of the number is “4294967295” instead of “-1”:
Scenario # 5: Using the Unsigned Integers as Loop Counters:
In the last scenario, we will be using the unsigned integer as the loop counter in C++. You can see this from the following code snippet:
In this code snippet, we have defined a simple “for” loop whose iterator is an unsigned integer, and we have decremented this unsigned integer in every iteration. We got stuck in an infinite loop when we executed this code, as you can see from the output shown below. This happened solely because in the last iteration when the value of the iterator was decremented to “-1”, instead of breaking the loop, this iterator was wrapped around to the nearest unsigned integer whose value was greater than “5”. This kept happening in every iteration, which is why we have this kind of output.
In this article, we wanted to talk about the various aspects of using the unsigned integers in C++ in Ubuntu 20.04. First, we shared the size of the unsigned integers in C++ with you, followed by their declaration. After that, we explained the different scenarios in which the usage of the unsigned integers should strictly be avoided by highlighting the issues they can cause. Therefore, after going through this article, you will easily differentiate between the situations in which the unsigned integers should and should not be used. We hope you found this article useful, and please check out Linux Hint for more informative articles.