C++ Primer Plus Chapter 3 Dealing with Data

Names for Variables

C++ encourages you to use meaningful names for variables.If a variable represents the cost of a trip,you should call it cost_of_trip or costOfTrip ,not just x or cot .You do have to follow a few simple C++ naming rules:

  • The only characters you can use in names are alphabetic characters,numeric digits, and the underscore (_) character.
  • The first character in a name cannot be a numeric digit.
  • Uppercase characters are considered distinct from lowercase characters.
  • You can’t use a C++ keyword for a name.
  • Names beginning with two underscore characters(__hehe) or with an underscore character followed by an uppercase letter(_Hehe) are reserved for use by the implementation—that is,
    the compiler and the resources it uses. Names beginning with a single underscore character are reserved for use as global identifiers by the implementation.
  • C++ places no limits on the length of a name,and all characters in a name are significant.However,some platforms might have their own length limits.

C++ Differs from ASCI C(C99):
ANSI C (C99) guarantees only that the first 63 characters in a name are significant.(In ANSI C,two names that have the same first 63 characters are considered identical,even if the 64th characters differ.)

有时变量名前会加上前缀用来指明变量的数据类型,如 n 表示 int,p 表示指针等等。

C++’s Build-in integer Data types

The differences between implementations for type widths can cause problems when you move a C++ program from one environment to another,including using a different compiler on the same system.

  • The <climits >file,which represents system limits for various integer types
  • sizeof operator returns the data size in bytes.
#include <iostream>
#include <climits>

/* C++'s Build-in integer types: unsigned long , long , unsigned int , int , unsigned short , short , char , unsigned char , signed char , bool
*/

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 <<" bytes."<<endl;
    cout<< "long long is "<< sizeof n_llong <<" bytes."<<endl;
    cout<<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<< "Minimun int value = "<<INT_MIN<<endl;
    cout<< "Bits per byte = "<< CHAR_BIT<<endl;

   return 0;
}

New C++11 Types: char16_t and char32_t

单个字符也可以占据不同长度

char16_t ch1 = u'q'; // basic character in 16-bit form
char32_t ch2 = U'\U0000222B'; // universal character name in 32-bit form
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值