我愛我的家園!

          成功在于你是否努力,希望在于你是否相信自己!

           

          C++常量的表示

          ?C++常量的表示

          ?
          ??? 一、char型常量

          ??? (一)單個字符:用單引號定界,如'a',C++轉(zhuǎn)義字符如下:
          ??????? (01)\n??????? 換行
          ??????? (02)\r??????? 回車
          ??????? (03)\t??????? 水平制表符
          ??????? (04)\v??????? 垂直制表符
          ??????? (05)\b??????? 退格
          ??????? (06)\a??????? 振鈴
          ??????? (07)\\??????? 字符\
          ??????? (08)\'??????? 字符'
          ??????? (09)\"??????? 字符"
          ??????? (10)\...????? 1-3位八進制ASCII碼代表的字符('\101')
          ??????? (11)\x.....?? 十六進制ASCII碼代表的字符,位數(shù)約束不嚴格,在字符串表示某個字符時請慎
          用之
          ??????? (12)\uxxxx??? 四位16進制數(shù)字
          ????????? 或\Uxxxxxxxx八位16進制數(shù)字實現(xiàn)通用符號(Universal character name)
          ????????? 注意,必須是四位或八位,位數(shù)少或多都會出錯。
          ???
          ??? (二)字符串,用雙引號定界,如"Code::Blocks",注意字符串的長度與占用內(nèi)存的大小,因字符串結(jié) 束標志為'\0',所以占用內(nèi)存的大小比實際可用長度多1個字符。字符串結(jié)束標志不必特別指明, "abcdeabcde\0",特別指明反而會再多占用一個字節(jié)的內(nèi)存空間。
          #include <iostream>
          #include <cstring>
          ???
          using namespace std;
          ?
          int main(int argc,char** argv) {
          ??? char s[13]="abcdeabcde";
          ??? char s1[]="abcdeabcde";
          ??? char s2[]="abcdeabcde\0\0\0\0";
          ??? cout<<sizeof(s)<<endl;
          ??? cout<<strlen(s)<<endl;
          ??? cout<<sizeof(s1)<<endl;
          ??? cout<<strlen(s1)<<endl;
          ??? cout<<sizeof(s2)<<endl;
          ??? cout<<strlen(s2)<<endl;????
          ??? return EXIT_SUCCESS;
          }
          //-----
          結(jié)果為:
          13
          10
          11
          10
          15
          10
          //-----
          ???
          ??? (三)wchar_t型常量加前綴L,只能是大寫L。如:
          ??????? wchar_t c=L'A';//這里的L可不加,看編譯器是否認可
          ??????? wchar_t cc[]=L"mytest";//這里的L必須加
          例:
          #include <iostream>
          using std::cout;
          using std::endl;
          int main(int argc,char** argv) {
          ??? wchar_t c=L'A';
          ??? char cc='A';
          ??? wchar_t cp[]=L"mytest";
          ??? char ccp[]="mytest1";
          ??? cout<<c<<endl;
          ??? cout<<(char)c<<endl;
          ??? cout<<cc<<endl;
          ??? cout<<cp<<endl;
          ??? cout<<ccp<<endl;
          ??? return EXIT_SUCCESS;
          }
          //-----
          65
          A
          A
          0x22ff60
          mytest1
          //-----
          ?
          ??? 二、整型常量

          ?
          ??? (一)書寫常量時,若不加任何修飾符,編譯器通常會認為是int整型,但有時也會根據(jù)操作自動實現(xiàn) 合適的轉(zhuǎn)換。
          #include <iostream>
          #include <climits>
          using namespace std;
          int main(int argc,char** argv) {
          ??? cout<<INT_MAX<<endl;
          ??? cout<<SHRT_MAX<<endl;
          ??? short x=32768;
          ??? short k=32767+1;
          ??? long long y=2147483647;
          ??? long long d=INT_MAX+1;
          ??? cout<<x<<endl;
          ??? cout<<k<<endl;
          ??? cout<<y<<endl;
          ??? cout<<d<<endl;
          ??? return EXIT_SUCCESS;
          }
          //-----
          2147483647
          32767
          -32768
          -32768
          2147483647
          -2147483648
          //-----
          ?
          ??? (二)無符號整型后綴是U(u),long型后綴是L(l),long long型后綴是LL(ll),L或LL或以與U組合。

          ??? 三、浮點型常量

          ??? 默認是double類型,表示float類型常量加后綴F(f),表示long double類型常量加后綴L(l)。
          ?
          ??? 四、布爾型常量只有true和false,只認小寫。當(dāng)然可以用常數(shù)來表示,0為假,非零為真。
          ?
          bool x=false;
          bool y=1.234;
          cout<<x<<endl;
          cout.setf(ios_base::boolalpha);
          cout<<y<<endl;
          //-----
          0
          true
          //-----
          ?
          例1:只有一個函數(shù),但調(diào)用時參數(shù)不同,編譯器盡量匹配
          #include <iostream>
          using namespace std;
          void show(long);
          int main(int argc,char** argv) {
          ??? show(123);
          ??? show(123L);
          ??? show(123UL);
          }
          void show(long demo) {
          ??? cout<<"long"<<endl;
          }
          //-----
          long
          long
          long
          //-----
          例2:有多個函數(shù),編譯器會認為是人為設(shè)計函數(shù)重載,將不再自動匹配
          #include <iostream>
          using namespace std;
          void show(int);
          void show(long);
          void show(unsigned long);
          int main(int argc,char** argv) {
          ??? show(123);
          ??? show(123L);
          ??? show(123UL);//改為show(123LL)試試如何?將會出錯。
          }
          void show(int demo) {
          ??? cout<<"int"<<endl;
          }
          void show(long demo) {
          ??? cout<<"long"<<endl;
          }
          void show(unsigned long demo) {
          ??? cout<<"unsigned long"<<endl;
          }
          //-----
          int
          long
          unsigned long
          //-----

          posted on 2008-09-05 22:24 死神 閱讀(1308) 評論(0)  編輯  收藏 所屬分類: C/C++

          導(dǎo)航

          統(tǒng)計

          公告

          歡迎大家來到我的個人世界!

          常用鏈接

          留言簿(3)

          隨筆分類(5)

          隨筆檔案(9)

          文章分類(37)

          文章檔案(41)

          相冊

          語音技術(shù)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 固原市| 蓬莱市| 宁陕县| 通海县| 安溪县| 沂南县| 马尔康县| 门源| 临潭县| 繁峙县| 莎车县| 元氏县| 林芝县| 象山县| 宜春市| 东乌| 遵义市| 齐齐哈尔市| 咸宁市| 桃园县| 仙桃市| 雅江县| 正镶白旗| 平塘县| 多伦县| 湛江市| 保德县| 万年县| 宽城| 东丽区| 神池县| 蒙阴县| 濮阳市| 芒康县| 海兴县| 集安市| 临城县| 尚志市| 南投市| 历史| 高淳县|