<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </object> <mce:style><!-- st1/:*{behavior:url(#ieooui) } --> <!-- [endif]--> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Comic Sans MS"; panose-1:3 15 7 2 3 3 2 2 2 4; mso-font-charset:0; mso-generic-font-family:script; mso-font-pitch:variable; mso-font-signature:647 0 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->
<!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->
<!-- [if supportFields]><span lang=EN-US style='font-family:宋体'><span style="mso-element:field-begin" mce_style="mso-element:field-begin"></span><span style='mso-spacerun:yes'> </span>= 4 /* GB2 <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]-->⑷ <!-- [if supportFields]><span lang=EN-US style='font-family:宋体'><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->关于自增运算符的进一步研究。
代码:
#include <stdio.h>
int main(int argc, char *argv[]){
int a,b;
a = b = 0;
b = ++a;
b = a++;
b++;
a = a++;
return 0;
}
反汇编后:
b = ++a;
00000039 inc dword ptr [ebp-0Ch]
0000003c mov eax,dword ptr [ebp-0Ch]
0000003f mov dword ptr [ebp-10h],eax
b = a++;
00000042 mov eax,dword ptr [ebp-0Ch]
00000045 mov dword ptr [ebp-10h],eax
00000048 inc dword ptr [ebp-0Ch]
b++;
0000004b inc dword ptr [ebp-10h]
a = a++;
0000004e nop
0000004f inc dword ptr [ebp-0Ch]
2010-1-30 晚
----------------------------------------cutting line----------------------------------------
<!-- [if supportFields]><span lang=EN-US style='font-family:宋体'><span style="mso-element:field-begin" mce_style="mso-element:field-begin"></span><span style='mso-spacerun:yes'> </span>= 5 /* GB2 <span style="mso-element:field-separator" mce_style="mso-element:field-separator"></span></span><![endif]-->⑸ <!-- [if supportFields]><span lang=EN-US style='font-family:宋体'><span style="mso-element:field-end" mce_style="mso-element:field-end"></span></span><![endif]-->C 中预处理与typedef 概览。
【目录】
1. 宏定义
2. 文件包含
3. 条件编译
4. typedef
一、宏定义
预处理命令,包括文件包含和条件编译,都是在预编译时进行处理的;因为编译器无法识别预处理命令,所以在编译前,预处理命令已经处理完毕。预处理命令是 ANSI C 统一规定的,但是并不是 C 语言本身的组成,所以纯粹的 C 编译器是无法识别它们的。
个人总结宏定义有 3 个用途:
I 简单的宏定义: #define A B
这个比较简单,就是纯粹的完全的替换
II 带参数的宏定义: #define A(a,b) a+b
这点有点类似函数处理,不过函数是在运行时进行调用,而宏定义在编译时便进行替换。前者时间花费在运行上的现场保护等,后者花费在预处理命令上的处理。
例子:
#include <stdio.h>
#define Add(a,b) a+b
int main(){
int c;
c = Add(2,3);
printf(“%d/n”,c);
return 0;
}
这一段代码输出 5 并进行换行。
III 得到多个结果的宏
有时候调用函数想返回多个值得使用数组或者指针,使用宏有时候可以简单地“返回”多个值。
例子:
#include <stdio.h>
#define LenAndArea(l,s,a,b) l=(a+b)*2;s=a*b;
int main(){
int len,area;
LenAndArea(len,area,2,3);
printf(“%d/n%d/n”,len,area);
return 0;
}
这一段输出 10 换行 6 换行
二、文件包含
写 C 语言第一句一般都是 #include <stdio.h> ,所以一般久而久之就以为这是 C 语言的一部分了;然而很惊人的,严格来说这不是 C 语言本身的组成部分。
文件包含有两种形式,一种是 #include “ stdio.h ”,表示要包含的文件从当前目录下搜索,而 #include <stdio.h> 则表示从存放 C 库函数头文件的目录搜索,为标准方式。
文件包含在编译预处理时形成一个新的源文件,即将目标文件全部包含进来再进行编译,形成 .obj 文件,再进行连接。
三、条件编译
条件编译给我的第一印象是类似于 ThinkPHP 的配置文件中设置了‘ DEBUG_MODE ’ => TRUE ,为什么呢?因为学习条件编译的时候看到的就是用来控制进行跟踪的输出。
例子:
#include <stdio.h>
#define DEBUG
int main(){
#ifdef DEBUG
printf(“ 开启了跟踪 /n”);
#endif
return 0;
}
当源文件中有多个跟踪输出的时候这样使用方便许多。此外,在避免重复包含头文件时也如此处理。
条件编译格式:
#ifdef A
code here…
#else
code
#endif
其中 #else 部分可以省略,只要前文中有 #define A 语句,甚至不需要对 A 进行具体定义标识,便会执行相应代码部分。
类似的有 #ifndef 。
条件编译与 if 语句不同,条件编译是在编译阶段选择是否进行编译,而 if 语句是程序运行阶段的控制语句。
最后还有一种条件编译,它根据表达式的值是否为真判断是否进行编译:
#if A
code1
#else
code2
#endif
如果 A 为真则编译 code1 ,否则编译 code2 。
四、 Typedef
typedef 不是预处理命令,它是在编译时进行处理的。
使用 typedef 可以利于程序的通用性和可移植性。有的机器上 int 类型占用 2 个字节,而有的机器上 int 类型占用 4 个字节,此时的 int 就相当于 long 类型的数值范围。为了便于移植,可以使用:
typedef int INT
typedef long INT
此外,使用 typedef 定义数组如下:
#include <stdio.h>
typedef int INTARR[10];
int main(){
INTARR a={1,2};
printf(“%d/n%d/n”,a[0],a[1]);
return 0;
}
2010-2-4 午
----------------------------------------cutting line----------------------------------------