Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Type Modifiers for Variables | Numerical Data Types
C++ Data Types

bookChallenge: 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

type_modifiers.h

copy
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

main.cpp

copy
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.

Note

There is a shorter syntax available you can use any of them:

  • short is equivalent to short int;
  • long is equivalent to long 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:

Opgave

Swipe to start coding

You are implementing a utility that estimates daily energy consumption for a city.

The function calculateEnergyConsumption should multiply a small per-person value by a (potentially) very large population and return the correct total — without losing information due to integer overflow.

  1. Use short int for averageConsumption — this small number (like 27 kWh) easily fits in a small-range type.
  2. Use int for cityPopulation, since populations can reach millions.
  3. Multiply averageConsumption by cityPopulation and store the result in totalConsumption.
  4. Use long int for totalConsumption, because the multiplication result may exceed the capacity of int.
  5. Return the total result from the function.

Do not modify the values of averageConsumption and cityPopulation inside the calculateEnergyConsumption function.

Example

averageConsumption = 27, cityPopulation = 3500000009450000000
averageConsumption = 25, cityPopulation = 200000050000000
averageConsumption = 40, cityPopulation = 1000004000000

Løsning

solution.cpp

solution.cpp

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

Awesome!

Completion rate improved to 4.35

bookChallenge: Type Modifiers for Variables

Stryg for at vise menuen

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

type_modifiers.h

copy
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

main.cpp

copy
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.

Note

There is a shorter syntax available you can use any of them:

  • short is equivalent to short int;
  • long is equivalent to long 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:

Opgave

Swipe to start coding

You are implementing a utility that estimates daily energy consumption for a city.

The function calculateEnergyConsumption should multiply a small per-person value by a (potentially) very large population and return the correct total — without losing information due to integer overflow.

  1. Use short int for averageConsumption — this small number (like 27 kWh) easily fits in a small-range type.
  2. Use int for cityPopulation, since populations can reach millions.
  3. Multiply averageConsumption by cityPopulation and store the result in totalConsumption.
  4. Use long int for totalConsumption, because the multiplication result may exceed the capacity of int.
  5. Return the total result from the function.

Do not modify the values of averageConsumption and cityPopulation inside the calculateEnergyConsumption function.

Example

averageConsumption = 27, cityPopulation = 3500000009450000000
averageConsumption = 25, cityPopulation = 200000050000000
averageConsumption = 40, cityPopulation = 1000004000000

Løsning

solution.cpp

solution.cpp

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
single

single

some-alt