锘??xml version="1.0" encoding="utf-8" standalone="yes"?> # 鍜?## 鎿嶄綔絎︽槸鍜?a href="mk:@MSITStore:E:\doc\C++%20璇█鍙傝?chm::/cppreference.com/preproc_details.html#define">#define瀹忎嬌鐢ㄧ殑. 浣跨敤# 浣垮湪#鍚庣殑棣栦釜鍙傛暟榪斿洖涓轟竴涓甫寮曞彿鐨勫瓧絎︿覆. 渚嬪, 鍛戒護(hù) 灝嗕細(xì)浣跨紪璇戝櫒鎶婁互涓嬪懡浠? 鐞嗚В涓? 浣跨敤##榪炵粨##鍓嶅悗鐨勫唴瀹? 渚嬪, 鍛戒護(hù) 灝嗕細(xì)浣跨紪璇戝櫒鎶? 瑙i噴涓? 鐞嗘墍褰撶劧,灝嗕細(xì)鍦ㄦ爣鍑嗚緭鍑哄鏄劇ず'10'. #line鍛戒護(hù)鏄敤浜庢洿鏀筥_LINE__ 鍜?__FILE__鍙橀噺鐨勫? 鏂囦歡鍚嶆槸鍙夌殑. __LINE__ 鍜?__FILE__ 鍙橀噺鎻忚堪琚鍙栫殑褰撳墠鏂囦歡鍜岃. 鍛戒護(hù) 鏇存敼琛屽彿涓?0,褰撳墠鏂囦歡鏀逛負(fù)"main.cpp". 璇硶:
]]>
棰勫鐞嗗懡浠?/h1>
#,##
#define to_string( s ) # s
cout << to_string( Hello World! ) << endl;
cout << "Hello World!" << endl;
#define concatenate( x, y ) x ## y
...
int xy = 10;
...
cout << concatenate( x, y ) << endl;
cout << xy << endl;
#line
璇硶:
#line line_number "filename"
#line 10 "main.cpp"
棰勫畾涔夊彉閲?/a>
__LINE__
__FILE__
__DATE__
__TIME__
_cplusplus
__STDC__
涓嬪垪鍙傛暟鍦ㄤ笉鍚岀殑緙栬瘧鍣ㄥ彲鑳戒細(xì)鏈夋墍涓嶅悓, 浣嗘槸涓鑸槸鍙敤鐨?
涓嬶紝鎵懼埌涓涓悕鍙玾inhex鐨勫伐鍏楓傛尯濂界敤鐨勶紝澶у鏈夎繖鏍風(fēng)殑
闂鐨勬椂鍊欏彲浠ョ敤涓涓嬨?br>鐚滄兂涓涓媤inhex鐨勬仮澶嶆寚瀹氱被鍨嬫枃浠跺伐浣滃師鐞?br>struct stBitMap
{
uint fileType;
uint ver;
uint size;
uint createDate;
}
鍋囧涓婇潰鏄竴涓綅鍥劇殑鏂囦歡緇撴瀯澶撮儴鍒嗭紝閭d箞winhex閫愭鎵?/p>
鎻忔枃浠跺潡姣旇緝 鍜岃繖涓粨鏋勯噷姣旇緝
In an unsigned type, all the bits represent the value. If a type is defined for a particular machine to use 8 bits, then the unsigned version of this type could hold the values 0 through 255.
鏃犵鍙峰瀷涓紝鎵鏈夌殑浣嶉兘琛ㄧず鏁板箋傚鏋滃湪鏌愮鏈哄櫒涓紝瀹氫箟涓縐嶇被鍨嬩嬌鐢?8 浣嶈〃紺猴紝閭d箞榪欑綾誨瀷鐨? unsigned 鍨嬪彲浠ュ彇鍊?0 鍒?255銆?/p>
The C++ standard does not define how signed types are represented at the bit level. Instead, each compiler is free to decide how it will represent signed types. These representations can affect the range of values that a signed type can hold. We are guaranteed that an 8-bit signed type will hold at least the values from 127 through 127; many implementations allow values from 128 through 127.
C++ 鏍囧噯騫舵湭瀹氫箟 signed 綾誨瀷濡備綍鐢ㄤ綅鏉ヨ〃紺猴紝鑰屾槸鐢辨瘡涓紪璇戝櫒鑷敱鍐沖畾濡備綍琛ㄧず signed 綾誨瀷銆傝繖浜涜〃紺烘柟寮忎細(xì)褰卞搷 signed 綾誨瀷鐨勫彇鍊艱寖鍥淬? 浣?signed 綾誨瀷鐨勫彇鍊艱偗瀹氳嚦灝戞槸浠?-127 鍒?127錛屼絾涔熸湁璁稿瀹炵幇鍏佽鍙栧間粠 -128 鍒?127銆?/p>
Under the most common strategy for representing signed integral types, we can view one of the bits as a sign bit. Whenever the sign bit is 1, the value is negative; when it is 0, the value is either 0 or a positive number. An 8-bit integral signed type represented using a sign-bit can hold values from 128 through 127.
琛ㄧず signed 鏁村瀷綾誨瀷鏈甯歌鐨勭瓥鐣ユ槸鐢ㄥ叾涓竴涓綅浣滀負(fù)絎﹀彿浣嶃傜鍙蜂綅涓?1錛屽煎氨涓鴻礋鏁幫紱絎﹀彿浣嶄負(fù) 0錛屽煎氨涓?0 鎴栨鏁般備竴涓?signed 鏁村瀷鍙栧兼槸浠?-128 鍒?127銆?/p>
The type of an object determines the values that the object can hold. This fact raises the question of what happens when one tries to assign a value outside the allowable range to an object of a given type. The answer depends on whether the type is signed or unsigned.
瀵硅薄鐨勭被鍨嬪喅瀹氬璞$殑鍙栧箋傝繖浼?xì)寮曡捣涓涓枒闂細(xì)褰撴垜浠瘯鐫鎶婁竴涓秴鍑哄叾鍙栧艱寖鍥寸殑鍊艱祴緇欎竴涓寚瀹氱被鍨嬬殑瀵硅薄鏃訛紝緇撴灉浼?xì)鎬庢牱鍛紵絳旀鍙栧喅浜庤繖縐嶇被鍨嬫槸 signed 榪樻槸 unsigned 鐨勩?/p>
For unsigned types, the compiler must adjust the out-of-range value so that it will fit. The compiler does so by taking the remainder of the value modulo the number of distinct values the unsigned target type can hold. An object that is an 8-bit unsigned char, for example, can hold values from 0 through 255 inclusive. If we assign a value outside this range, the compiler actually assigns the remainder of the value modulo 256. For example, we might attempt to assign the value 336 to an 8-bit signed char. If we try to store 336 in our 8-bit unsigned char, the actual value assigned will be 80, because 80 is equal to 336 modulo 256.
瀵逛簬 unsigned 綾誨瀷鏉ヨ錛岀紪璇戝櫒蹇呴』璋冩暣瓚婄晫鍊間嬌鍏舵弧瓚寵姹傘傜紪璇戝櫒浼?xì)灏嗚鍊煎 unsigned 綾誨瀷鐨勫彲鑳藉彇鍊兼暟鐩眰妯★紝鐒跺悗鍙栨墍寰楀箋傛瘮濡?8 浣嶇殑 unsigned char錛屽叾鍙栧艱寖鍥翠粠 0 鍒? 255錛堝寘鎷?255錛夈傚鏋滆祴緇欒秴鍑?guó)櫩欎釜鑼冨洿鐨勫鹼紝閭d箞緙栬瘧鍣ㄥ皢浼?xì)鍙栬鍊煎 256 姹傛ā鍚庣殑鍊箋備緥濡傦紝濡傛灉璇曞浘灝?336 瀛樺偍鍒?8 浣嶇殑 unsigned char 涓紝鍒欏疄闄呰祴鍊間負(fù) 80錛屽洜涓?80 鏄?336 瀵?256 姹傛ā鍚庣殑鍊箋?/p>
For the unsigned types, a negative value is always out of range. An object of unsigned type may never hold a negative value. Some languages make it illegal to assign a negative value to an unsigned type, but C++ does not.
瀵逛簬 unsigned 綾誨瀷鏉ヨ錛岃礋鏁版繪槸瓚呭嚭鍏跺彇鍊艱寖鍥淬?tt>unsigned 綾誨瀷鐨勫璞″彲鑳芥案榪滀笉浼?xì)淇濆瓨璐熸暟銆傛湁浜涜璦涓皢璐熸暟璧嬬粰 unsigned 綾誨瀷鏄潪娉曠殑錛屼絾鍦?C++ 涓繖鏄悎娉曠殑銆?/p>
In C++ it is perfectly legal to assign a negative number to an object with unsigned type. The result is the negative value modulo the size of the type. So, if we assign 1 to an 8-bit unsigned char, the resulting value will be 255, which is 1 modulo 256. C++ 涓紝鎶婅礋鍊艱祴緇?unsigned 瀵硅薄鏄畬鍏ㄥ悎娉曠殑錛屽叾緇撴灉鏄璐熸暟瀵硅綾誨瀷鐨勫彇鍊間釜鏁版眰妯″悗鐨勫箋傛墍浠ワ紝濡傛灉鎶?-1 璧嬬粰8浣嶇殑 unsigned char錛岄偅涔堢粨鏋滄槸 255錛屽洜涓?255 鏄?-1 瀵?256 姹傛ā鍚庣殑鍊箋?/p> |
When assigning an out-of-range value to a signed type, it is up to the compiler to decide what value to assign. In practice, many compilers treat signed types similarly to how they are required to treat unsigned types. That is, they do the assignment as the remainder modulo the size of the type. However, we are not guaranteed that the compiler will do so for the signed types.
褰撳皢瓚呰繃鍙栧艱寖鍥寸殑鍊艱祴緇?signed 綾誨瀷鏃訛紝鐢辯紪璇戝櫒鍐沖畾瀹為檯璧嬬殑鍊箋傚湪瀹為檯鎿嶄綔涓紝寰堝鐨勭紪璇戝櫒澶勭悊 signed 綾誨瀷鐨勬柟寮忓拰 unsigned 綾誨瀷綾諱技銆備篃灝辨槸璇達(dá)紝璧嬪兼椂鏄彇璇ュ煎璇ョ被鍨嬪彇鍊兼暟鐩眰妯″悗鐨勫箋傜劧鑰屾垜浠笉鑳戒繚璇佺紪璇戝櫒閮戒細(xì)榪欐牱澶勭悊 signed 綾誨瀷銆?/p>
浠ヤ笂鎽樿嚜 c++ primer,鎯劎錛岃繕鏄啀緇嗙粏鐨勪粠澶村搧涓嬈¤繖涔﹀惂銆?br>
棣栧厛瀛︿範(fàn)涓や釜鍑芥暟
|