variables and types(变量和数据类型)

9 篇文章 0 订阅
  1. 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. (最小存储单位)
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. scope

    • a name’s scope is from its declaration to the end of current function body, always differed by { }.
  7. 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.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值