72.8k views
3 votes
Write a Program to print the size and range of values ​​of integer and real number data types in c++

2 Answers

3 votes

Answer:

#include <iostream>

#include <limits>

int main() {

std::cout << "Size of integer types:\\";

std::cout << "=======================\\";

std::cout << "sizeof(char): " << sizeof(char) << " byte(s)\\";

std::cout << "sizeof(short): " << sizeof(short) << " byte(s)\\";

std::cout << "sizeof(int): " << sizeof(int) << " byte(s)\\";

std::cout << "sizeof(long): " << sizeof(long) << " byte(s)\\";

std::cout << "sizeof(long long): " << sizeof(long long) << " byte(s)\\";

std::cout << "\\";

std::cout << "Range of integer types:\\";

std::cout << "=======================\\";

std::cout << "char: " << static_cast<int>(std::numeric_limits<char>::min()) << " to "

<< static_cast<int>(std::numeric_limits<char>::max()) << "\\";

std::cout << "short: " << std::numeric_limits<short>::min() << " to " << std::numeric_limits<short>::max() << "\\";

std::cout << "int: " << std::numeric_limits<int>::min() << " to " << std::numeric_limits<int>::max() << "\\";

std::cout << "long: " << std::numeric_limits<long>::min() << " to " << std::numeric_limits<long>::max() << "\\";

std::cout << "long long: " << std::numeric_limits<long long>::min() << " to "

<< std::numeric_limits<long long>::max() << "\\";

std::cout << "\\";

std::cout << "Size of real number types:\\";

std::cout << "==========================\\";

std::cout << "sizeof(float): " << sizeof(float) << " byte(s)\\";

std::cout << "sizeof(double): " << sizeof(double) << " byte(s)\\";

std::cout << "sizeof(long double): " << sizeof(long double) << " byte(s)\\";

std::cout << "\\";

std::cout << "Range of real number types:\\";

std::cout << "==========================\\";

std::cout << "float: " << std::numeric_limits<float>::lowest() << " to " << std::numeric_limits<float>::max() << "\\";

std::cout << "double: " << std::numeric_limits<double>::lowest() << " to " << std::numeric_limits<double>::max() << "\\";

std::cout << "long double: " << std::numeric_limits<long double>::lowest() << " to "

<< std::numeric_limits<long double>::max() << "\\";

return 0;

}

Step-by-step explanation:

This program uses the <iostream> and <limits> headers to print out the size and range of values for the different data types. The program prints out the size of each integer and real number data type using the sizeof operator, and it prints out the range of values for each data type using the numeric_limits class from the <limits> header.

User Adam Coulombe
by
8.7k points
6 votes

Answer:

#include <iostream>

#include <limits>

using namespace std;

int main() {

// integer data types

cout << "Size and range of integer data types:" << endl;

cout << "------------------------------------" << endl;

cout << "char: " << sizeof(char) << " bytes, "

<< static_cast<int>(numeric_limits<char>::min()) << " to "

<< static_cast<int>(numeric_limits<char>::max()) << endl;

cout << "short: " << sizeof(short) << " bytes, "

<< numeric_limits<short>::min() << " to "

<< numeric_limits<short>::max() << endl;

cout << "int: " << sizeof(int) << " bytes, "

<< numeric_limits<int>::min() << " to "

<< numeric_limits<int>::max() << endl;

cout << "long: " << sizeof(long) << " bytes, "

<< numeric_limits<long>::min() << " to "

<< numeric_limits<long>::max() << endl;

cout << "long long: " << sizeof(long long) << " bytes, "

<< numeric_limits<long long>::min() << " to "

<< numeric_limits<long long>::max() << endl;

// real number data types

cout << endl;

cout << "Size and range of real number data types:" << endl;

cout << "---------------------------------------" << endl;

cout << "float: " << sizeof(float) << " bytes, "

<< numeric_limits<float>::min() << " to "

<< numeric_limits<float>::max() << endl;

cout << "double: " << sizeof(double) << " bytes, "

<< numeric_limits<double>::min() << " to "

<< numeric_limits<double>::max() << endl;

cout << "long double: " << sizeof(long double) << " bytes, "

<< numeric_limits<long double>::min() << " to "

<< numeric_limits<long double>::max() << endl;

return 0;

}

Step-by-step explanation:

The program uses the sizeof operator to determine the size of each data type in bytes, and the numeric_limits class template from the <limits> header to print the minimum and maximum values for each data type. The static_cast<int> is used to cast the char data type to an int so that its minimum and maximum values can be printed as integers.

User Wigy
by
8.7k points