- 1
Give me examples of the signed and unsigned modifiers in integers in c++
5 odpowiedzi
+ 6
They are data type modifiers and as the name implies they modify the type of data. Usually the memory size.
For example short int, long int.
The signed int doesn't exist.. maybe you meant..
signed char --> -128 ~ +127 range
Only char has signed, because it's signed by default.
An int is always unsigned by default, you can use unsigned modifier to shift the range to 0 up to 4,294,967,295 while a normal integer of the same size (4bytes usually) is from -2,...,...,... to +2.......
The range size is always the same but you can reach a bigger value using unsigned.
If you don't need negative values it's very useful, for example in iterators, but in this case I give you a tip..
std::size_t is the most used type for iterators, it is designed for such purpose.
Hope it helps.
+ 1
Unsigned int is seldom use, but doesn't mean it's not exists.
For example, some input variable have to be positive and could be large number, we would use unsigned int, like srand(unsigned int seed)
Check the input parameter of srand function
http://www.cplusplus.com/reference/cstdlib/srand/
+ 1
Don't be confused, in C++ int is signed int by default
- 1
And also write the outputs
- 1
int i; // signed modifier
unsigned int i; // unsigned int
Don't use unsigned int, you don't need such large range of integer, it could create issue when the program set the modifier to negative value.