Long and short ? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Long and short ?

what are they? are they integeds? when would they be used?

6th Feb 2017, 12:59 PM
Mich
Mich - avatar
6 ответов
+ 2
suppose you are writing a program of simple Armstrong number that you can see to my profile. in this case you know that a user can input as long as the value greater than 2^15. here comes the role of long int. similarly as per your perception of the input taken by the user you have to use long wisely!
6th Feb 2017, 1:22 PM
GeekyShacklebolt
GeekyShacklebolt - avatar
+ 2
so use is based on experience . based on the range you indicated, right? 2 to 15 bytes? or am I misinterpretating? I appreciate your help. the more I engage with these concepts and put them into practice, the more it will click. thank you very much .
6th Feb 2017, 1:25 PM
Mich
Mich - avatar
+ 1
there is something in C++ which is called "type specifier". -type specifier are of 2 types. 1. sign specifier 2. size specifier long and short fall under the category of size specifier. size specifier changes the byte size of a variable. e.i:- int a; //by default it is "short int" 2 BYTES. //range 2^15 but if we need to store an integer num of greater value than its range. then we can increase int byte size using Long specifier... as long int b; //it takes 4 BYTES, range 2^31. see its simple... similarly to increase float byte size we use... long float c; // takes 8 bytes long double; //takes 16 bytes
6th Feb 2017, 1:14 PM
GeekyShacklebolt
GeekyShacklebolt - avatar
+ 1
thank you, CodeRunner. how do we know when we would put these to use?
6th Feb 2017, 1:19 PM
Mich
Mich - avatar
+ 1
I mean 2 to 15 as a range of value stored in a variable. not bytes.
6th Feb 2017, 1:26 PM
Mich
Mich - avatar
+ 1
Nice answer @CodeRunner, except for a small error: Ranges for: short int is 2^16, not 2^15 long int is 2^32, not 2^31 It is easier to see on a small number - lets say 3 bits. The range for a 3 bit number is 2^3 (i.e. 8), not 2^2 (i.e. 4) since the range is 0 to 7. From the quality of your answer I actually suspect you knew this, but probably (I'm guessing here!) just got confused with the max (unsigned value) which will fit into a number - which is 2^16-1 for 16 bits (but note it is not 2^(16-1) which is what you specified). Either way, I think you probably know this!
6th Feb 2017, 1:35 PM
Ettienne Gilbert
Ettienne Gilbert - avatar