我愛我的家園!

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

           

          C++常量的表示

          ?C++常量的表示

          ?
          ??? 一、char型常量

          ??? (一)單個字符:用單引號定界,如'a',C++轉義字符如下:
          ??????? (01)\n??????? 換行
          ??????? (02)\r??????? 回車
          ??????? (03)\t??????? 水平制表符
          ??????? (04)\v??????? 垂直制表符
          ??????? (05)\b??????? 退格
          ??????? (06)\a??????? 振鈴
          ??????? (07)\\??????? 字符\
          ??????? (08)\'??????? 字符'
          ??????? (09)\"??????? 字符"
          ??????? (10)\...????? 1-3位八進制ASCII碼代表的字符('\101')
          ??????? (11)\x.....?? 十六進制ASCII碼代表的字符,位數約束不嚴格,在字符串表示某個字符時請慎
          用之
          ??????? (12)\uxxxx??? 四位16進制數字
          ????????? 或\Uxxxxxxxx八位16進制數字實現通用符號(Universal character name)
          ????????? 注意,必須是四位或八位,位數少或多都會出錯。
          ???
          ??? (二)字符串,用雙引號定界,如"Code::Blocks",注意字符串的長度與占用內存的大小,因字符串結 束標志為'\0',所以占用內存的大小比實際可用長度多1個字符。字符串結束標志不必特別指明, "abcdeabcde\0",特別指明反而會再多占用一個字節的內存空間。
          #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;
          }
          //-----
          結果為:
          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整型,但有時也會根據操作自動實現 合適的轉換。
          #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,只認小寫。當然可以用常數來表示,0為假,非零為真。
          ?
          bool x=false;
          bool y=1.234;
          cout<<x<<endl;
          cout.setf(ios_base::boolalpha);
          cout<<y<<endl;
          //-----
          0
          true
          //-----
          ?
          例1:只有一個函數,但調用時參數不同,編譯器盡量匹配
          #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:有多個函數,編譯器會認為是人為設計函數重載,將不再自動匹配
          #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++

          導航

          統計

          公告

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

          常用鏈接

          留言簿(3)

          隨筆分類(5)

          隨筆檔案(9)

          文章分類(37)

          文章檔案(41)

          相冊

          語音技術

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 五莲县| 徐汇区| 托克托县| 本溪市| 乐业县| 龙胜| 左云县| 苍山县| 普定县| 封开县| 丽水市| 兴国县| 九台市| 库车县| 邓州市| 恭城| 章丘市| 阿拉善左旗| 东城区| 玛多县| 崇义县| 平定县| 东乡县| 海宁市| 子长县| 林甸县| 奉新县| 武冈市| 绵竹市| 威远县| 共和县| 阳城县| 丹东市| 沈阳市| 洪洞县| 呼图壁县| 赤城县| 山丹县| 确山县| 兴化市| 屏边|