C/C ++ knowledge points 4-printf function and cout calculation sequence, printfcout

Source: Internet
Author: User

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:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.