早教吧 育儿知识 作业答案 考试题库 百科 知识分享

C++的运算问题(高手请进!)WhatisthevalueofkandjifaprogrammewiththefollowingC/C++codeinitsmainfunctionbodyisexecuted?Youcanwriteasimpleprogrammeandrunittohelpyougettheanswer.Trytoexplainwhy.intm=9;intn=9

题目详情
C++的运算问题(高手请进!)
What is the value of k and j if a programme with the following C/C++ code in its main function body is executed?You can write a simple programme and run it to help you get the answer.Try to explain why.
int m=9;
int n=9;
int k,j;
k=--n + --n ;
j=m+(++m-10);
cout
▼优质解答
答案和解析
只说一句,__int64和 pucxx解释得很清楚了,而且受益.
之后又通过验证,猜想如下:
int n=18;
int j;
j=--n+ --n;
//j=16+16=32
j=--n+ --n+ --n;
//j=15+13+13=41
j=--n+ --n+ --n+ --n;
//j=12+11+9+9=41
j==--n+ --n+ --n+ --n+ --n;
//j=8+7+6+4+4=29
j==--n+ --n+ --n+ --n+ --n+ --n;
//j=3+2+1+0+(-2)+(-2)=2;
相信大家都看出规律了吧,编译器默认将最后两个表达式的值同时取n做自减运算后的最终值,当然这只是猜测,因为没有办法“逐表达式”调试.但我相信我是对的!