自增
(1)后缀:与Turbo C相同,在语句结束之前或者说分号之前才会执行自增。
(2)前缀:
前两个自增统一取值,后面的自增即为取值。
int i=2,j; j=++i+(++i)+(++i);
j=4+4+5;
注意不能直接写j=++i+++i+++i;
否则会报错error C2105: '++' needs l-value;
因为++后面跟的还是+,必须要左值才能++;
int i=2,j;
j=(i++)++;如此也同样会报错error C2105: '++' needs l-value;
因为i++返回的是一个数值 ,而不是一个可赋值的变量(左值);
统一取值必须是指前两个并且必须是连在一起的,否则每一个都是自增即为取值。
int i=,j;
j=++i+i+++(++i)+(++i);j=3+3+4+5;
无论是int 还是 float都遵循这个规则。
(3)在乘法中的使用规则:
i++*++i自动识别为++i*i++;
int i=,j1,j2;
j1=i++*++i;
i=;
j2=++i*i++;j1=3*3;
j1==j2;
自减也是同样的。
《裘宗燕:C/C++语言中的表达式求值》文章摘要:
另外,想要知道变量更新的值是否已经保存到了内存,需要注意顺序点在哪里;
C/C++语言定义(语言的参考手册)明确定义了顺序点的概念。顺序点位于:
1.每个完整表达式结束时。完整表达式包括变量初始化表达式,表达式语句,return语句的表达式,以及条件、循环和switch语句的控制表达式(for头部有三个控制表达式);
2.运算符&&、||、?:和逗号运算符的第一个运算对象计算之后;
3.函数调用中对所有实际参数和函数名表达式(需要调用的函数也可能通过表达式描述)的求值完成之后(进入函数体之前)。
而且C/C++并没有对所有的运算顺序进行规定,而是交由编译器根据需要实现调整,从而得到效率更高的代码;
Java则有严格规定表达式求值顺序,但大部分程序设计语言实际上都才用了类似C/C++的规定。
“谁知道下面C语句给n赋什么值?”
m=1;n=m+++m++;
正确回答是:不知道!语言没有规定它应该算出什么,结果完全依赖具体系统在具体上下文中的具体处理。
其中牵涉到运算对象的求值顺序和变量修改的实现时刻问题。
评论区摘要:
②c99:
AnnexJ
J.1Unspecifiedbehavior:
—Theorderinwhichsubexpressionsareevaluatedandtheorderinwhichsideeffectstakeplace,
exceptasspecifiedforthefunction-call(),&&,||,?:,andcommaoperators(6.5).
—Theorderinwhichthefunctiondesignator(操作数指示符),arguments(实参),andsubexpressions(子表达式) withintheargumentsareevaluatedinafunctioncall(6.5.2.2).
—Theorderinwhichtheoperandsofanassignmentoperator(赋值运算符的操作数) areevaluated(被求值) (6.5.16).
........
J.2Undefinedbehavior
—Betweentwosequencepoints(序列点/顺序点),anobjectismodified(修正) morethanonce,orismodified
andthepriorvalueisreadotherthan(除了) todeterminethevaluetobestored(6.5).
......
④c++0x最终草案(FDIS)
ISO/IECFDIS14882
N3290:
1.9Programexecution(执行)
15Exceptwherenoted,evaluationsofoperandsofindividualoperatorsandofsubexpressionsofindividual
expressionsareunsequenced. [ Note:Inanexpressionthatisevaluatedmorethanonceduringtheexecution
ofaprogram,unsequencedandindeterminatelysequencedevaluationsofitssubexpressionsneednotbeperformed
consistently(一贯地)indifferentevaluations.—endnote]Thevaluecomputationsoftheoperandsof
anoperatoraresequencedbeforethevaluecomputationoftheresultoftheoperator.Ifasideeffectona
scalarobject(标量对象)isunsequencedrelativetoeitheranother sideeffect onthesamescalarobject or
avaluecomputationusingthevalueofthesamescalarobject,thebehaviorisundefined.
[Example:
voidf(int,int);
voidg(inti,int*v){
i=v[i++];//thebehaviorisundefined
i=7,i++,i++;//ibecomes9
i=i+++1;//thebehaviorisundefined
i=i+1;//thevalueofiisincremented(递增)
f(i=-1,i=-1);//thebehaviorisundefined
}
—endexample]
Whencallingafunction(whetherornotthefunctionisinline(内联)[内联函数继承了宏定义的优点,也消除了它的缺点]),
everyvaluecomputationandsideeffectassociatedwithanyargumentexpression,orwiththepostfixexpression
designating(把...定名为) thecalledfunction, issequencedbeforeexecutionofevery
expressionorstatement inthebodyofthecalledfunction.[Note:Value computationsandsideeffects
associatedwithdifferentargumentexpressionsare
unsequenced.—endnote.]
Everyevaluationinthecallingfunction(includingotherfunctioncalls)thatisnototherwisespecifically
sequencedbeforeoraftertheexecutionofthebody
ofthecalledfunctionisindeterminatelysequencedwithrespecttotheexecutionofthecalledfunction.9Several
contextsinC++causeevaluationofafunctioncall,eventhoughnocorrespondingfunctioncallsyntaxappearsi
nthetranslationunit.
[Example:Evaluationofanewexpressioninvokes(借助) oneormoreallocation andconstructorfunctions;see5.3.4.
Foranotherexample,invocationofaconversionfunction(12.3.2)canarisein contexts(上下文)inwhichnofunctioncall
syntaxappears.—endexample]Thesequencingconstraints(约束) ontheexecutionofthecalledfunction(asdescribedabove)
arefeaturesofthefunctioncallsasevaluated,whateverthesyntaxoftheexpressionthatcallsthefunction might be.
总结:尽量不要写容易发生误会的表达式,因为运算顺序本身就没有严格规定,所以不要迷信编译器,不要迷信执行结果,有些事情确实是无法定论的。所以之前总结的运算规律,
也只适合考试的时候看看,要真正掌握原理,还是需要用VC时按Alt+8、Alt+6和Alt+5,开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和
寄存器变化(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化)。
而且有时候老师说的东西也是不准确的,面对什么问题,都需要有一种刨根问底的精神。
参考:http://blog.csdn.net/chenbang110/article/details/9192595
参考:http://bbs.csdn.net/topics/370153775