//c++第二章1.1-3.2
//1)变量名的命名
//c++名称的长度没有限制,所有字符都有意义,可以用下划线或者大写字母隔开eeg:my_book_list or myBookList
//_xx,__xx,_X,被保留给实现使用
//2)数据类型
//【整型】基本整型10种:char -> short -> int -> long -> long long (画unsinged ,有符号)
//short至少16位,int至少与short一样长,long至少32位,long long h至少64位
//CHAR_BIT,INT_MAX等表示符号常量被定义在climits文件中
//最好将变量的声明和赋值分开
//usinged是usinged int的缩写
#include <iostream>
#include <climits>
int main()
{
// insert code here...
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
//cout
cout << "int is " << sizeof(int) << "bytes. " << endl;//对类型名使用sizeof 使用运算符,必须括号
cout << "short is "<< sizeof(n_short) << "bytes. "<< endl;//对变量名使用sizeof 预算符
cout << "long is " << sizeof(n_long) << "bytes. "<< endl;
cout << "long long is "<< sizeof n_llong<< "bytes. "<< endl;对变量名使用sizeof 预算符,括号可有可无
cout << endl;
cout << "Maximum value: "<<endl;
cout << "int: "<<n_int <<endl;
cout << "short: "<< n_short << endl;
cout << "long: "<< n_long << endl;
cout <<"long long : "<<n_llong <<endl;
cout <<"minimum value= "<< INT_MIN << endl;
cout <<"Bits per byte = "<<CHAR_BIT <<endl;
cout <<endl;
int emus{7};
int rheas = {12};
int test = {};
cout <<"emus = " << emus <<endl;
cout << "rheas = "<< rheas <<endl;
cout << "test = "<< test <<endl;
char ch = 'M';
int i=ch;//i的类型确定了
cout << "add ASCII code for" <<ch <<" is "<<i <<endl;
cout << "add one to the character code: "<<endl;
ch = ch+1;
i=ch;
cout <<"The ASCII code for "<<ch << "is" <<i <<endl;
cout << "desplay char ch using cout.put(ch): ";
cout.put(ch);//函数显示一个字符
cout.put('!');
cout<< endl<<"Done"<<endl;
cout << "ben \"Buggsie\" Hacker\nwas here!\n";
std::cout << "Hello, World!\n";
return 0;
}
int is 4bytes.
short is 2bytes.
long is 8bytes.
long long is 8bytes.
Maximum value:
int: 2147483647
short: 32767
long: 9223372036854775807
long long : 9223372036854775807
minimum value= -2147483648
Bits per byte = 8
实验结果:
emus = 7
rheas = 12
test = 0
add ASCII code forM is 77
add one to the character code:
The ASCII code for Nis78
desplay char ch using cout.put(ch): N!
Done
ben "Buggsie" Hacker
was here!
Hello, World!
Program ended with exit code: