标签:tin $1 情况 方式 end 有符号数 lan 根据 处理器
Cpp primer plus notes
using namespace 编译指令。
void关键字显示说明不接受任何参数。
//myfirst.cpp
#include <iostream>
int main(void)
{
using namespace std;
cout << "Come up and c++ me some time.";// << : 为插入运算符,将字符串插入到输出流当中. [cout对象]
cout << endl;
cout << "You won‘t regret it!" << endl;
return 0;
}
// << 运算符被重载, 同一个符号可能有多重含义,编译器根据上下文确定其具体功能.
// endl 是C++ 的特殊符号. 控制符:重启一行.在iostream中定义 位于std namespace中. [ manipulator ] 等同于:\n
// 差别在于endl保证程序继续运行前刷新输出, 使用\n不能保证.
main函数被启动代码调用,启动代码是编译器添加到程序中的。在C语言中main()函数省略返回值类型,默认是返回int,在C++中弃用了这种用法,显式声明。
// cpp的输入输出工具, 需要包含如下两行代码 cin cout包含在iostream中.
// 预处理指令作用: 将iostream文件内容添加到程序中; 在源码编译之前,添加或者替换文本.
#include <iostream>
using namespace std;
#include <iostream>
int main()
{
using namespace std;
int carrots; // 变量声明 指定变量类型和存储名称. defining declaration == definition编译器会为该变量分配内存空间 C++首次使用前声明
carrots = 25;
cout << "I have ";
cout << carrots;
cout << " carrots.";
cout << endl;
carrots = carrots - 1;
cout << "Crunch, crunch. Now I have " << carrots << " carrots." << endl;
return 0;
}
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots; // cin也是一个智能对象 需要右侧提供一个变量抽取信息
cout << "Here are two more. ";
carrots += 2;
cout << "Noew you have " << carrots << " carrots." << endl;
return 0;
}
// cin 是 istream类对象 也是在iostream中定义的.
在使用函数之前C++必须知道函数的参数类型和返回值类型.
#include <iostream>
#include <cmath>
int main()
{
using namespace std;
double area;
cout << "Enter the floor area, in square feet, of your home: ";
cin >> area;
double side;
side = sqrt(area);
cout << "That‘s the equivalent of a square " << side
<< " feet to the side." << endl;
cout << "How fascinating!" << endl;
return 0;
}
#include <iostream>
void simon(int);
int main()
{
using namespace std;
simon(3);
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count);
cout << "Done!" << endl;
return 0;
}
void simon(int n)
{
using namespace std;
cout << "Simon says touch your toes " << n << " times" << endl;
}
#include <iostream>
int stonetolb(int);
int main()
{
using namespace std;
int stone;
cout << "Enter the weight in stone: ";
cin >> stone; // >> 抽取运算符
int pounds = stonetolb(stone);
cout << stone << " stone = ";
cout << pounds << " pounds." << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
变量名: 精确,统一.
ANSI C99变量名63个字符长度;
整型:char、 short(至少16bit) 、int、 long(至少32bit) 、long long(至少64bit c++11)
sizeof运算符获取变量位宽. 单位字节.
#define 也是预处理器编译指令
#include <iostream>
#include <climits>
int main()
{
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 << "int is " << sizeof(int) << " bytes." << endl;
cout << "short is " << sizeof n_short << " bytes." << endl;
cout << "long is " << sizeof n_long << " buyes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << "Maximum values:" << endl;
cout << "int: " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long: " << n_llong << endl << endl;
cout << "Mimium int value = " << INT_MIN << endl;
cout << "Bits per byte = " << CHAR_BIT << endl;
return 0;
}
// sizeof的运算符. 对于变量来说括号()是可选的 !
C++初始化语法
int wrens(423); // int wrens = 423;
int wrens = {423};
int wrens{423};
int wrens = {}; // 大括号是空的场景,变量将被初始化为0. 【大括号初始化器 ---- 使常规变量与类变量方式更像】
int wrens{};
警告:如果不对函数内部定义的变量进行初始化,该变量的值是不确定的,这意味着该变量的值将是它被创建之前,相应内存单元保存的值.【残留值】
#include <iostream>
#include <climits>
#define ZERO 0
int main()
{
using namespace std;
short sam = SHRT_MAX;
unsigned short sue = sam;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposited." << endl
<< "Add $1 to each account." << endl << "Now ";
sam += 1; // 翻转为负值最小值 -32768
sue += 1;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposites.\nPoor Sam!" << endl;
sam = ZERO;
sue = ZERO;
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposites." << endl;
cout << "Take $1 form each account." << endl << "Now ";
sam -=1;
sue -=1; // 翻转为最大值 65535
cout << "Sam has " << sam << " dollars and Sue has " << sue;
cout << " dollars deposites.\nLucky Sue!" << endl;
return 0;
}
[root@localhost cpp]# ./a.out
Sam has 32767 dollars and Sue has 32767 dollars deposited.
Add $1 to each account.
Now Sam has -32768 dollars and Sue has 32768 dollars deposites.
Poor Sam!
Sam has 0 dollars and Sue has 0 dollars deposites.
Take $1 form each account.
Now Sam has -1 dollars and Sue has 65535 dollars deposites.
Lucky Sue!
无论是有符号数还是无符号数,整型最大值和最小值相邻的 +1 -1转换;
对于short 无符号 0 - 1 --> 65535
对于short有符号 32767 + 1 --> -32768 -32768 - 1 --> 32767
注意 上溢和下溢;
自然长度int 如果没有特别理由选择int作为变量类型, 处理效率最高。
//3.3
#include <iostream>
int main()
{
using namespace std;
int chest = 42;
int waist = 0x42;
int inseam = 042;
cout << "Monsieur cuts a striking figure!\n";
cout << "chest = " << chest << " (42 in decimal)\n";
cout << hex; // 改变显示字面值的base 16进制 控制符std::hex 修改显示整数的方式
cout << "waist = " << waist << " (0x42 in hex)\n";
cout << oct; // 改变显示字面值的base 8进制
cout << "inseam = " << inseam << " (042 in octal)\n";
return 0;
}
默认情况下cout以十进制显示整数.
C++如何确定常量类型:
除非有理由存储为其他类型(特殊后缀,数值太大) C++将整形常量存储为int.
//p49 程序清单3.6
标签:tin $1 情况 方式 end 有符号数 lan 根据 处理器
原文地址:https://www.cnblogs.com/xuperior/p/14765748.html