C++课堂练习二

1、数据类型及其所占字节数

#include<iostream>
#include<string>
using namespace std;

int main(){
    cout << "***************************************" << endl;
    cout << "int:"<< sizeof(int) << endl; 
    cout << "short int:"<< sizeof(short int) << endl;
    cout << "long int:"<< sizeof(int) << endl; 
    cout << "unsigned int:"<< sizeof(unsigned int) << endl;
    cout << "unsigned short int:"<< sizeof(unsigned short int) << endl;
    cout << "unsigned long int:"<< sizeof(unsigned long int) << endl;  
    cout << "char:"<< sizeof(char) << endl;
    cout << "unsigned char:"<< sizeof(unsigned char) << endl;
    cout << "bool:"<< sizeof(bool) << endl;
    cout << "double:"<< sizeof(double) << endl;  
    cout << "float:"<< sizeof(float) << endl;
    cout << "***************************************" << endl;
    cout << "A:" << sizeof(A) << endl;
    cout << "1:" << sizeof(1) << endl;
    cout << "1.0:" << sizeof(1.0) << endl;
    cout << "True:" << sizeof(true) << endl;
    cout << "hello World:" << sizeof("Hello World") << endl;
    cout << "程序:" << sizeof("程序") << endl;
    cout << "***************************************" << endl;
    return 0;
} 

分享图片

#include<iostream>
#include<string>
#include<limits> 
using namespace std;

int main(){
    cout << "int\t\t\t所占字节数:" << sizeof(int)<< "\t\tmin:" << (numeric_limits<int>::min)() << "\t\tmax:" << (numeric_limits<int>::max)()<< endl;
    cout << "short int\t\t所占字节数:" << sizeof(short int)<< "\t\tmin:" << (numeric_limits<short int>::min)() << "\t\tmax:" << (numeric_limits<short int>::max)()<< endl;
    cout << "long int\t\t所占字节数:" << sizeof(long int) << "\t\tmin:" << (numeric_limits<long int>::min)() << "\t\tmax:" << (numeric_limits<long int>::max)()<< endl;
    cout << "unsigned int\t\t所占字节数:" << sizeof(unsigned int)<< "\t\tmin:" << (numeric_limits<unsigned int>::min)() << "\t\t\tmax:" << (numeric_limits<unsigned int>::max)()<< endl;
    cout << "unsigned short int\t所占字节数:" << sizeof(unsigned short int)<< "\t\tmin:" << (numeric_limits<unsigned short int>::min)() << "\t\t\tmax:" << (numeric_limits<unsigned short int>::max)()<< endl;
    cout << "unsigned long int\t所占字节数:" << sizeof(unsigned long int)<< "\t\tmin:" << (numeric_limits<unsigned long int>::min)() << "\t\t\tmax:" << (numeric_limits<unsigned long int>::max)()<< endl;
    cout << "char\t\t\t所占字节数:" << sizeof(char) << "\t\tmin:" << (numeric_limits<char>::min)() << "\t\t\tmax:" << (numeric_limits<char>::max)()<< endl;
    cout << "signed char\t\t所占字节数:" << sizeof(signed char)<< "\t\tmin:" << (numeric_limits<signed char>::min)() << "\t\t\tmax:" << (numeric_limits<signed char>::max)()<< endl;
    cout << "unsigned char\t\t所占字节数:" << sizeof(unsigned char)<< "\t\tmin:" << (numeric_limits<unsigned char>::min)() << "\t\t\tmax:" << (numeric_limits<unsigned char>::max)()<< endl;
    cout << "bool\t\t\t所占字节数:" << sizeof(bool)<< "\t\tmin:" << (numeric_limits<bool>::min)() << "\t\t\tmax:" << (numeric_limits<bool>::max)()<< endl;
    cout << "string\t\t\t所占字节数:" << sizeof(string)<< "\t\tmin:" << (numeric_limits<string>::min)() << "\t\t\tmax:" << (numeric_limits<string>::max)()<< endl;
    cout << "double\t\t\t所占字节数:" << sizeof(double)<< "\t\tmin:" << (numeric_limits<double>::min)() << "\tmax:" << (numeric_limits<double>::max)()<< endl;
    cout << "float\t\t\t所占字节数:" << sizeof(float)<< "\t\tmin:" << (numeric_limits<float>::min)() << "\tmax:" << (numeric_limits<float>::max)()<< endl;
    return 0;
} 

分享图片

相关文章
相关标签/搜索