Home > Back-end >  Calculate the
Calculate the

Time:11-13

Why is 22, I was 21, who don't understand, o

CodePudding user response:

Undefined behavior, the different compiler, the result is not necessarily the same

CodePudding user response:

Should be like this: the first part of the value of the expression: (+ + a) + (a + + a) regardless of the first half and the second half of the
Two twice since the + + operator that increase 1 to use its value so 7 + 7
In the second part evaluates the expression in front of (+ + a) + (a + + a) is the result so will not be behind a + + 14 + 8==22

CodePudding user response:

Many compilation system regulation, continuous multiple prefix + + and - operators in the same operation expression, continuous variables since the first add or decrease the N times, and then determine a + + + a value of N, supposedly, output statements should be implemented: first, scan the whole operation expression q=(a + + a) + (a + + a) + (a + + a), found that there are three + + prefix type usage, and so will a since 1 three times in a row, to 8, and then began to calculation expression, the first bracket is + + a determination of 8, the second bracket is + + a decision value of 8, a third bracket is + + a decision value of 8, finally, the calculation results of 8 + 8 + 8=24, the experiment is as follows:
 int main (int arg c, char * argv []) 
{
Int a=5, q=0;
Q=(a + + a) + (a + + a) + (a + + a);
Printf (" % d \ n ", a);
Printf (" % d \ n ", q);
system("pause");
return 0;
}



Do you still really didn't want to understand the equal to 22,

CodePudding user response:

reference QZJHJXJ reply: 3/f
many compilation system regulation, continuous multiple prefix + the + and - operators in the same operation expression, continuous variables since the first add or decrease the N times, and then determine a + + + a value of N, supposedly, output statements should be implemented: first, scan the whole operation expression q=(a + + a) + (a + + a) + (a + + a), found that there are three + + prefix type usage, and so will a since 1 three times in a row, to 8, and then began to calculation expression, the first bracket is + + a determination of 8, the second bracket is + + a decision value of 8, the third parenthesis is + + a decision value of 8, finally, the calculation results of 8 + 8 + 8=24, the experiment is as follows:
 int main (int arg c, char * argv []) 
{
Int a=5, q=0;
Q=(a + + a) + (a + + a) + (a + + a);
Printf (" % d \ n ", a);
Printf (" % d \ n ", q);
system("pause");
return 0;
}



Do you still really didn't want to understand this is 22,


Suggest the contents stipulated by the compiler, didn't see too ~

CodePudding user response:

Expression evaluation order undefined, so 6 + 7 + 7 + 8 + 8, 7, 8 + 8 + 8 May

CodePudding user response:

references 4 building self-confidence boy reply:
Quote: refer to the third floor QZJHJXJ response:
many compilation system regulation, continuous multiple prefix + the + and - operators in the same operation expression, continuous variables since the first add or decrease the N times, and then determine a + + + a value of N, supposedly, output statements should be implemented: first, scan the whole operation expression q=(a + + a) + (a + + a) + (a + + a), found that there are three + + prefix type usage, and so will a since 1 three times in a row, to 8, and then began to calculation expression, the first bracket is + + a determination of 8, the second bracket is + + a decision value of 8, a third bracket is + + a decision value of 8, finally, the calculation results of 8 + 8 + 8=24, the experiment is as follows:
 int main (int arg c, char * argv []) 
{
Int a=5, q=0;
Q=(a + + a) + (a + + a) + (a + + a);
Printf (" % d \ n ", a);
Printf (" % d \ n ", q);
system("pause");
return 0;
}



Do you still really didn't want to understand this is 22,


Suggest the contents stipulated by the compiler, haven't seen ~

Is not the same as the above, each compiler

CodePudding user response:

refer to 6th floor qq_50192543 response:
Quote: refer to 4th floor confident boy reply:
Quote: refer to the third floor QZJHJXJ response:
many compilation system regulation, continuous multiple prefix + the + and - operators in the same operation expression, continuous variable since the first add or decrease the N times, and then determine a + + + a value of N, supposedly, output statements should be implemented: first, scan the whole operation expression q=(a + + a) + (a + + a) + (a + + a), found that there are three + + prefix type usage, and so will a since 1 three times in a row, to 8, and then began to calculation expression, the first bracket is + + a determination of 8, the second bracket is + + a decision value of 8, the third parenthesis is + + a decision value of 8, finally, the calculation results of 8 + 8 + 8=24, the experiment is as follows:
 int main (int arg c, char * argv []) 
{
Int a=5, q=0;
Q=(a + + a) + (a + + a) + (a + + a);
Printf (" % d \ n ", a);
Printf (" % d \ n ", q);
system("pause");
return 0;
}



Do you still really didn't want to understand this is 22,


Suggest the contents stipulated by the compiler, haven't seen ~

Is not the same as the above, each compiler

"A lot of compiling system regulation, continuous multiple prefix + the + and - operators in the same operation expression, to continuous variables from the add or decrease the N times, and then determine the + + a value of a + N"
For this sentence have not seen, I mean where to find,
I also know that multiple + + (on the) - (from) reduction has side effects, different compilers calculation result is different, you this sentence meaning most compilers disposed of in accordance with the method of the you say, so in theory, most compilers results should be the same

CodePudding user response:

It's really nothing to headache, also nothing to tangle, practice, run into this problem, you're not going to use, now as a study, to study, test, analyze, to deepen understanding c statement, also have not cannot.
  • Related