Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Type Modifiers for Variables | Numerical Data Types
C++ Data Types

Свайпніть щоб показати меню

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

Завдання

Swipe to start coding

  1. Change the type of the variables so it can hold a larger number.
  2. Output the result of the expression.

Рішення

solution.cpp

solution.cpp

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

Awesome!

Completion rate improved to 4.35

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

Завдання

Swipe to start coding

  1. Change the type of the variables so it can hold a larger number.
  2. Output the result of the expression.

Рішення

solution.cpp

solution.cpp

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

close

Awesome!

Completion rate improved to 4.35

Свайпніть щоб показати меню

some-alt