C + + data types and ranges of their values

Source: Internet
Author: User

Common types in C + + are bool, char, short, int, long, double, float, string, and so on. These data types meet the needs that we become everyday.

The following code prints the number of bytes and their maximum and minimum values corresponding to each data type.

1#include <iostream>2#include <string>3#include <limits>4#include <climits>5 using namespacestd;6 7 intMain ()8 {9cout <<"bool: \t\t"<<"number of bytes in the account:"<<sizeof(BOOL);Tencout <<"\ t Maximum value:"<< (numeric_limits<BOOL>:: Max) (); Onecout <<"\t\t\t Minimum Value:"<< (numeric_limits<BOOL>::min) () <<Endl; Acout <<"Char: \t\t"<<"number of bytes in the account:"<<sizeof(Char); -cout <<"\ t Maximum value:"<< (numeric_limits<Char>:: Max) (); -cout <<"\t\t\t Minimum Value:"<< (numeric_limits<Char>::min) () <<Endl; thecout <<"signed char: \ t"<<"number of bytes in the account:"<<sizeof(SignedChar); -cout <<"\ t Maximum value:"<< (numeric_limits<signedChar>:: Max) (); -cout <<"\t\t\t Minimum Value:"<< (numeric_limits<signedChar>::min) () <<Endl; -cout <<"unsigned char: \ t"<<"number of bytes in the account:"<<sizeof(unsignedChar); +cout <<"\ t Maximum value:"<< (numeric_limits<unsignedChar>:: Max) (); -cout <<"\t\t\t Minimum Value:"<< (numeric_limits<unsignedChar>::min) () <<Endl; +cout <<"wchar_t: \ t"<<"number of bytes in the account:"<<sizeof(wchar_t); Acout <<"\ t Maximum value:"<< (numeric_limits<wchar_t>:: Max) (); atcout <<"\t\t\t Minimum Value:"<< (Numeric_limits<wchar_t>::min) () <<Endl; -cout <<"Short : \t\t"<<"number of bytes in the account:"<<sizeof( Short); -cout <<"\ t Maximum value:"<< (numeric_limits< Short>:: Max) (); -cout <<"\t\t\t Minimum Value:"<< (numeric_limits< Short>::min) () <<Endl; -cout <<"int: \t\t"<<"number of bytes in the account:"<<sizeof(int); -cout <<"\ t Maximum value:"<< (numeric_limits<int>:: Max) (); incout <<"\t\t Minimum Value:"<< (numeric_limits<int>::min) () <<Endl; -cout <<"unsigned: \ t"<<"number of bytes in the account:"<<sizeof(unsigned); tocout <<"\ t Maximum value:"<< (numeric_limits<unsigned>:: Max) (); +cout <<"\t\t Minimum Value:"<< (Numeric_limits<unsigned>::min) () <<Endl; -cout <<"Long: \t\t"<<"number of bytes in the account:"<<sizeof(Long); thecout <<"\ t Maximum value:"<< (numeric_limits<Long>:: Max) (); *cout <<"\t\t Minimum Value:"<< (numeric_limits<Long>::min) () <<Endl; $cout <<"unsigned long: \ t"<<"number of bytes in the account:"<<sizeof(unsignedLong);Panax Notoginsengcout <<"\ t Maximum value:"<< (numeric_limits<unsignedLong>:: Max) (); -cout <<"\t\t Minimum Value:"<< (numeric_limits<unsignedLong>::min) () <<Endl; thecout <<"double: \ t"<<"number of bytes in the account:"<<sizeof(Double); +cout <<"\ t Maximum value:"<< (numeric_limits<Double>:: Max) (); Acout <<"\t\t Minimum Value:"<< (numeric_limits<Double>::min) () <<Endl; thecout <<"long double: \ t"<<"number of bytes in the account:"<<sizeof(Long Double); +cout <<"\ t Maximum value:"<< (numeric_limits<Long Double>:: Max) (); -cout <<"\t\t Minimum Value:"<< (numeric_limits<Long Double>::min) () <<Endl; $cout <<"float: \t\t"<<"number of bytes in the account:"<<sizeof(float); $cout <<"\ t Maximum value:"<< (numeric_limits<float>:: Max) (); -cout <<"\t\t Minimum Value:"<< (numeric_limits<float>::min) () <<Endl; -cout <<"size_t: \ t"<<"number of bytes in the account:"<<sizeof(size_t); thecout <<"\ t Maximum value:"<< (numeric_limits<size_t>:: Max) (); -cout <<"\ t Minimum value:"<< (Numeric_limits<size_t>::min) () <<Endl;Wuyicout <<"string: \ t"<<"number of bytes in the account:"<<sizeof(string); thecout <<"\ t Maximum value:"<< (numeric_limits<string>:: Max) (); -cout <<"\t\t\t Minimum Value:"<< (numeric_limits<string>::min) () <<Endl; Wucout <<"long long: \ t"<<"number of bytes in the account:"<<sizeof(Long Long); -cout <<"\ t Maximum value:"<< (numeric_limits<unsignedLong Long>:: Max) (); Aboutcout <<"\ t Minimum value:"<< (numeric_limits<unsignedLong Long>::min) () <<Endl; $     return 0; -}

The running results of the program are as shown.

  The following conclusions can be drawn from the running results of the program:

    • BOOL has only two values, which is true and false, and can be represented in the C + + language with 1 and zero respectively
    • There are three types of data that can represent decimals: double, long double, and float, which can account for 8-, 16-, and 4-bit values, respectively
    • The largest range of integer data types is long long, which is commonly used in ACM contests to handle large numbers

  The above results are supplemented by the following explanations:

    • wchar_t is a 8-bit character type in C + + that can represent a range far beyond Char
    • size_t is the type of data returned by the sizeof identifier, typically used for looping, array indexing, size storage, and address arithmetic. Its length is the same as long long, but it does not appear in the program to represent a numeric variable

C + + data types and ranges of their values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.