Regarding Unsigned, Signed etc | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Regarding Unsigned, Signed etc

i still don't understand what is the purpose for unsigned, signed, short, long and what is it for. could anyone convert it to a simplest way to make me understand?

26th Sep 2016, 5:05 AM
Yeo HaoWei
Yeo HaoWei - avatar
8 Answers
+ 1
When a variable is stored in memory, the memory it takes depends on its data type. A long int will take more space than an int, and a short int will take less. The number of different values that a variable stored on n bytes can take is 2^(8*n). Signed values can be negative, unsigned can't but have double the space for positive ones. Type Size (bytes) Range-Minimum Range- Maximum Character 1 -128 127 Unsigned Char 1 0 255 Signed Char 1 -128 127 Integer 4 -2,147,483,648 2,147,483,647 Signed Int 4 0 4,294,967,295 Unsigned Int 4 -2,147,483,648 2,147,483,647 Short Int 2 -32,768 32,767 Signed short Int 2 0 65,535 Unsigned Short Int 2 -32,768 32,767 Long Int 4 -2,147,483,648 2,147,483,647 Signed Long Int 8 0 4,294,967,295 Unsigned Long Int 8 -2,147,483,648 2,147,483,647 Float 4 1.8E-38 3.4E+38 Double 8 2.2E-308 1.8E+308 Long Double 8 2.2E-308 1.8E+308 Bool 1 True/False True/False Wchar_t 2 or 4 0 65,535 Trying to "just write down the number" would mean erasing the memory adjacent to the variable if the number is too big, so no, we can't just do that.
26th Sep 2016, 8:52 AM
Zen
Zen - avatar
0
It's all about memory. Declaring a variable you tell the program how much memory you want to reserve for it. Or the way you want it to save the number. Long or short, if you want more or less memory for that variable, changing how big the number memorized in it can be. Signed if you want to memorized either positive or negative numbers. Unsigned only positive numbers, but not having negative ones, you have space for double quantity of positive numbers. e.g. if you have 4 bit of memory, you can save 16 numbers. signed: from -8 to +7 unsigned: from 0 to 15 long: 4 or 8 bit, 16 or 256 numbers (depending) short: 2 or 4 bit, 4 or 16 numbers (depending)
26th Sep 2016, 7:54 AM
marcram
0
so it's just changing the memory? so what does the int hold? and what it you going trough big number? just weird why cant we just writ down the number
26th Sep 2016, 7:58 AM
Yeo HaoWei
Yeo HaoWei - avatar
0
When a program start, it has to know, and you have to choose, how much memory to reserve. The INT one: it depends on the compiler, on the computer, on the operating system. Actually, it should be 32bit, from −2.147.483.648 to +2.147.483.647. Short = 16 bit, long = int = 32bit. But depends. You can use also "long long int", 64bit, it's a very long one...
26th Sep 2016, 8:11 AM
marcram
0
can i just say that if I'm making a game and it requires quite alot of memory so i put long int for a reserve. it could hold more memory so i won't be afraid that it might exceed?
26th Sep 2016, 8:44 AM
Yeo HaoWei
Yeo HaoWei - avatar
0
In your game, it depends on what every variable has to do. Some variables will probably store only "2 digits" numbers, others big numbers, others fractional numbers. Using very big variables to store small numbers will result in too big programs. Maybe not now, but when you will create that game you'll probably know which type to use. Experience will teach you.
26th Sep 2016, 9:17 AM
marcram
0
marcram, sorry i have another question, if i write just int 10, but it cant hold negative number? when u write int 10 its already a positive them why must we put unsigned or signed.
27th Sep 2016, 3:32 AM
Yeo HaoWei
Yeo HaoWei - avatar
0
Because, if you have an expression that result in a number that you know will be between 0 and 40000, for example, you can declare a variable INT. or you can save memory and declare a SHORT UNSIGNED INT, that can hold numbers between 0 and 65535. Perhaps it will be useful when you have to make your program more light... or perhaps you'll never have to use it.
27th Sep 2016, 4:33 AM
marcram