學(xué)習(xí)點(diǎn)
- 命名空間
- 變量
- 枚舉
問題
定義了變量類型為unsigned肃弟,為啥還能傳遞負(fù)數(shù)進(jìn)去阱当?
為啥float類型的數(shù)據(jù)打印的時(shí)候沟突,精度丟失组底?
為啥double類型的數(shù)據(jù)打印的時(shí)候丈积,精度丟失?
使用extern的變量定義债鸡,作用是啥江滨?
show me the code
#include <iostream>
/**
* 引入命名空間
*/
using namespace std;
/**
* 函數(shù)聲明
*/
void learn_namespace_and_cout();
void learn_var();
void learn_enum();
/**
* 全局變量
*/
extern int extern_int;
int main() {
learn_namespace_and_cout();
learn_var();
learn_enum();
return 0;
}
void learn_namespace_and_cout() {
/**
* 打印經(jīng)典的 hello world
* 打印 int 類型
*/
cout << "----------[learn_namespace_and_cout]----------" << endl;
cout << "hello world" << endl;
cout << 1 << endl;
}
void learn_var() {
// 類型 長度 范圍
// char 1byte -127 - 127 或者 0 - 255
// unsigned char 1byte 0 - 255
// signed char 1byte -127 - 127
// int 4bytes -2147483648 - 2147483647
// unsigned int 4bytes 0 - 4294967295
// signed int 4bytes -2147483648 - 2147483647
// short int 2bytes -32768 - 32767
// unsigned short int Range 0 - 65,535
// signed short int Range -32768 - 32767
// long int 4bytes -2,147,483,647 - 2,147,483,647
// signed long int 4bytes 和 long int 一樣
// unsigned long int 4bytes 0 - 4,294,967,295
// float 4bytes +/- 3.4e +/- 38 (~7 位數(shù)字)
// double 8bytes +/- 1.7e +/- 308 (~15 位數(shù)字)
// long double 8bytes +/- 1.7e +/- 308 (~15 位數(shù)字)
// wchar_t 2 或者 4 bytes 1 個(gè)寬字符
/**
* C++變量類型
* 單精度是這樣的格式,1位符號(hào)厌均,8位指數(shù)唬滑,23位小數(shù)
* 雙精度是1位符號(hào),11位指數(shù)棺弊,52位小數(shù)
*
* 當(dāng)你使用多個(gè)文件晶密,并且你自己定義的變量放在其中一個(gè)文件里,變量的聲明將對(duì)程序的鏈接很有用模她。
* 您可以使用 extern 關(guān)鍵字來聲明一個(gè)放在任何位置的變量稻艰。
*
* TODO 定義了變量類型為unsigned,為啥還能傳遞負(fù)數(shù)進(jìn)去侈净?
* TODO 為啥float類型的數(shù)據(jù)打印的時(shí)候尊勿,精度丟失?
* TODO 為啥double類型的數(shù)據(jù)打印的時(shí)候用狱,精度丟失运怖?
* TODO 使用extern的變量定義,如果值發(fā)生變化夏伊,是否會(huì)全局發(fā)生變化?
*
*
*/
cout << "----------[learn_var]----------" << endl;
char _char = 0;
cout << _char << endl;
cout << (int) _char << endl;
cout << "sizeof:" << sizeof(_char) << endl;
unsigned char _unsigned_char = -1;
cout << _unsigned_char << endl;
cout << "-----[learn_var_float]-----" << endl;
float _float = 1.1111111111111;
cout << _float << endl;
double _double = 1.1111111111111;
cout << _double << endl;
float _float_no_initialization;
cout << _float_no_initialization << endl;
cout << "-----[learn_extern]-----" << endl;
int extern_int;
extern_int = 99;
cout << extern_int << endl;
int extern_int_int_01_25 = 1;
cout << extern_int_int_01_25 << endl;
}
void learn_enum() {
/**
* 定義枚舉
* 枚舉的默認(rèn)賦值是從0開始的吻氧,依次類推
*/
cout << "----------[learn_enum]----------" << endl;
enum TEST_ENUM {
A, B, C = 6, D
} x;
cout << A << endl;
cout << B << endl;
cout << C << endl;
x = D;
cout << x << endl;
}