c++ 的整形字面值和如何确定常量类型

时间:2020-05-29
本文章向大家介绍c++ 的整形字面值和如何确定常量类型,主要包括c++ 的整形字面值和如何确定常量类型使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include <iostream>
using namespace std;
int main()
{	
   //整形
   //short至少是16bit
   //int至少与short一样,这里是32bit
   //long至少是32bit,至少与int一样长
   //longlong 至少是64,至少与long一样长

   //c++ 通过数字后边的字母来识别常量类型
   cout << "c++通过数字后边的字母识别常量类型" << endl;
   //数字后边加L或者l,意思这是个long类型
   long lValue = 10l;
   cout << "long lValue 10L :" << lValue <<endl;
   //数字后边加U或者u,标志这是个无符号int
   unsigned int uValue = 10u;
   cout << "unsigned 10u is :" << uValue << endl;
   //数字后边带ul表示这是个unsigned long,还有unsigned long long的ull,大小写不敏感
   unsigned long ulValue = 10ul;
   cout << "ulValue 10ul is :" << ulValue << endl;
   //数字后边什么也不带,默认识别成int类型
   int intValue = 10;
   cout << "intValue 10 is :" << intValue << endl << endl; 



   cout << "c++ 通过数字前面的前缀识别进制形式" << endl;
   //数字后边什么也不加,表示是十进制表示的数字。
   int emptyValue = 20;
   cout << endl << "emptyValue 10 is :" << emptyValue << endl;
   //数字前边加0,表示该数值是用8进制表示的
   int zeroValue = 010;
   cout << endl << "010 is :" << zeroValue<<endl;
   cout << oct << "oct 010 is :" << zeroValue<<endl;
   //数字前边加0x,表示该数值是用16进制表示的
   int  hexValue = 0x10;
   cout << endl << "0x10 is :" <<  hexValue << endl;
   cout << hex << "hex 0x10 is :" <<  hexValue << endl;


   cout << "c++ primer plus page 45." << endl;

   return 0;
}

$ ./4.out             
c++通过数字后边的字母识别常量类型
long lValue 10L :10
unsigned 10u is :10
ulValue 10ul is :10
intValue 10 is :10

c++ 通过数字前面的前缀识别进制形式

emptyValue 10 is :20

010 is :8
oct 010 is :10

0x10 is :20
hex 0x10 is :10

原文地址:https://www.cnblogs.com/feipeng8848/p/12989938.html