C/C ++ knowledge points 4-printf function and cout calculation sequence, printfcout
Calculation sequence of the printf function: First press the stack from right to left, then the stack from left to right.
Routine:
#include"stdio.h"int main(){int arr[] = { 1, 2, 3, 4, 5 };int *ptr = arr;printf("%d %d\n", *ptr, *(++ptr));return 0;}
Output: 2, 2
Computing sequence: Calculate * (++ ptr), press the stack, then calculate * (ptr), and then press the stack.
Note: The calculation sequence of ++ ptr and ptr ++ may lead to different results.
#include"stdio.h"int main(){int arr[] = { 1, 2, 3, 4, 5 };int *ptr = arr;printf("%d %d\n", *ptr, *(ptr++));return 0;}
Output: 2, 1
Computing sequence: First press the stack * (ptr), then execute the ++ operation, and then press the stack * (ptr). During output, the stack is output from left to right, so the result is 2, 1.
The same is true for cout.
See routine:
#include"iostream"using namespace std;int main(){int arr[] = { 1, 2, 3, 4, 5 };int *ptr = arr;cout << *(ptr) << " " << *(ptr++) << " "<<*(ptr++)<<endl;return 0;}
Output: