C-language side effects!! Don't be careful!

Source: Internet
Author: User

Today just read the book side effects, bloggers think, side effect is actually changing the value of the variable, that is, an assignment operation! But just in the know still made a mistake!! Awkward!!

As you know, the assignment operator in C is a combination from right to left!!

Here is an example of a side effect in assignment:

#include <stdio.h>

int main ()

{

int a = 3;

A + = a = A*a;

printf ("%d\n", a);

return 0;

}

Did you think of the answer? Does anyone think the answer is-3?

The small partner has made the same mistake as the blogger, the positive solution should be this:
A==3
The first step: a*a==9;
Step two: Execute a-= 9; (Note there is an assignment, which is the side effect we're talking about)
Results: a==-6;
Step Three: Execute a + =-6;
Results: a==-12;

So the execution result should be-12; Try it on the machine!

After all, practice is the only standard to test truth!!

C-language side effects!! Don't be careful!

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.