Challenge: Type Modifiers for Variables
The keywords short and long are type modifiers. They are used to modify the size or range of a data type. They don't create new data types but rather alter the properties of existing types.
type_modifiers.h
1234567// `short` is used for variables // That require smaller range of values. short int small_range_integer_variable; // `long` is used for variables // That require a larger range of values. long int large_range_integer_variable;
Sometimes, you know that the values will certainly be small. For example, when storing the age of users, the value won't exceed 255. Such values can fit within 8 bits.
main.cpp
12345678910#include <iostream> int main() { short int small_number = 45; long int large_number = 4000000000; std::cout << "Small number: " << small_number <<std:: endl; std::cout << "Large number: " << large_number << std::endl; }
What those type modifiers do is change the size of a type. While int takes up 4 bytes, short int takes up 2 bytes, and the long int 8 bytes of memory.
There is a shorter syntax available you can use any of them:
shortis equivalent toshort int;longis equivalent tolong int;
So, we need to use long (long int) to store large values. In contrast, we can use short (short int) to take up less memory. However, its range is narrower because of that. Here is the table with ranges that a type can hold:
Swipe to start coding
The calculateEnergyConsumption function multiplies a small per-person value by a large population without causing overflow.
- Store
averageConsumptionin ashort int. - Store
cityPopulationin anint. - Multiply them and store the result in a
long intnamedtotalConsumption. - Return
totalConsumption. - Do not change the values of
averageConsumptionorcityPopulationinside the function.
Do not modify the values of averageConsumption and cityPopulation inside the calculateEnergyConsumption function.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain when to use short, int, or long in real-world scenarios?
What happens if I try to store a value outside the range of a type?
Are there unsigned versions of these types, and how do their ranges differ?
Awesome!
Completion rate improved to 4.35
Challenge: Type Modifiers for Variables
Swipe to show menu
The keywords short and long are type modifiers. They are used to modify the size or range of a data type. They don't create new data types but rather alter the properties of existing types.
type_modifiers.h
1234567// `short` is used for variables // That require smaller range of values. short int small_range_integer_variable; // `long` is used for variables // That require a larger range of values. long int large_range_integer_variable;
Sometimes, you know that the values will certainly be small. For example, when storing the age of users, the value won't exceed 255. Such values can fit within 8 bits.
main.cpp
12345678910#include <iostream> int main() { short int small_number = 45; long int large_number = 4000000000; std::cout << "Small number: " << small_number <<std:: endl; std::cout << "Large number: " << large_number << std::endl; }
What those type modifiers do is change the size of a type. While int takes up 4 bytes, short int takes up 2 bytes, and the long int 8 bytes of memory.
There is a shorter syntax available you can use any of them:
shortis equivalent toshort int;longis equivalent tolong int;
So, we need to use long (long int) to store large values. In contrast, we can use short (short int) to take up less memory. However, its range is narrower because of that. Here is the table with ranges that a type can hold:
Swipe to start coding
The calculateEnergyConsumption function multiplies a small per-person value by a large population without causing overflow.
- Store
averageConsumptionin ashort int. - Store
cityPopulationin anint. - Multiply them and store the result in a
long intnamedtotalConsumption. - Return
totalConsumption. - Do not change the values of
averageConsumptionorcityPopulationinside the function.
Do not modify the values of averageConsumption and cityPopulation inside the calculateEnergyConsumption function.
Solution
Thanks for your feedback!
single