byte and word
- Byte is the basic unit you can get address in memory, which consists of 8 bits. (最小寻址单位)
- Word is the basic unit you do the storage in the memory, which consists of 32 or 64 bits. (最小存储单位)
how to choose type
- int or long long.
- char may be signed or unsigned in diffrent machine. When you want to use char for a relatively small value, please assign it as signed char or unsigned char.
- double is more prevalent with a much higher accuracy and similar cost in computation. long double is useless in practice.
type convertion
- assign float to int, the decimals would be removed.
- assign unsigned type a out of range number, the result would be a modulus in this unsigned range.
- assign signed type a out of range number, the results would be undefined.
declaration and definition
- declaration tell computer the type and name, but definition apply space in memory for this variable.
- if u want declare a variable without definition, u can add extern before it, ex,
- extern int i; declaration
- int i; definition
- extern int i = 0; definition. u can use obvious initialization to break the declaration brought by extern.
- u can declare a varibale many times but only define it once. also, u can not initialize a extern variable inisde a funciton.
initialization
- int i = 0; int i = {0}; int i(0); int i{0};
- {0} is called list initialization, if there is unstable information in your deifinition, program will report errors.
- double pi = 3.14; int i = {pi}; would be wrong, but int i = pi; would be ok;
- for a internal type in c++, if u define a variable outside any function body, it would be initialized as default. in function, it is undefined.
- for a variable you define, whether default initialization or not would depend on the class u defined.
scope
- a name’s scope is from its declaration to the end of current function body, always differed by { }.
reference
- reference is a compound variable, but not a object. it is just another name of an existing object. after defined, all the operations for this reference would be done to the object refered by it.
- int ival = 1; int &ref = val; reference must be initialized and must be initialized by an object with the same type.
variables and types(变量和数据类型)
最新推荐文章于 2024-08-12 11:19:11 发布