Compound assignment operators in C Language

Source: Internet
Author: User

The compound assignment operators include + =,-=, * =,/=, % =, <=, >>=, & =, ^ =, | =.

The following uses + = as an example.

A + = expression (1)

It is equivalent:

A = a + expression (2)

However, there is a difference here. (1) expression A (such as the element accessed by the array subscript) is evaluated only once, and (2) expression A is evaluated twice, of course, the compiler may be optimized, and the effect of formula (2) may be the same as that of formula (1). However, in the case of exceptions, the compiler cannot be optimized. For example:

int f(int x){return x;} int a[10] = {0};int x = 2; a[f(x) + 1] += 1;              // (1)a[f(x) + 1] = a[f(x) +1] +1;      // (2)

The machine code of the above two expressions is completely different. For the (2) formula, the compiler cannot determine whether the same value is returned for each function because the function is called (that is, whether there are side effects ), therefore, the compiler cannot do anything about this and cannot optimize it. Therefore, the compiler will call the function twice, while the (1) method only calls the compile function. For details, refer to the assembly code under vs2010:

    a[f(x) + 1] = a[f(x) +1] +2;013B1413  mov        eax,dword ptr [ebp-3Ch] 013B1416  push       eax 013B1417  call       @ILT+110(_f) (13B1073h) 013B141C  add        esp,4 013B141F  mov        esi,dword ptr [ebp+eax*4-2Ch] 013B1423  add        esi,2 013B1426  mov        ecx,dword ptr [ebp-3Ch] 013B1429  push       ecx 013B142A  call       @ILT+110(_f) (13B1073h) 013B142F  add        esp,4 013B1432  mov        dword ptr [ebp+eax*4-2Ch],esi     a[f(x) + 1] += 2;013B1436  mov        eax,dword ptr [ebp-3Ch] 013B1439  push       eax 013B143A  call       @ILT+110(_f) (13B1073h) 013B143F  add        esp,4 013B1442  lea         ecx,[ebp+eax*4-2Ch] 013B1446  mov        dword ptr [ebp-104h],ecx 013B144C  mov        edx,dword ptr [ebp-104h] 013B1452  mov        eax,dword ptr [edx] 013B1454  add        eax,2 013B1457  mov        ecx,dword ptr [ebp-104h] 013B145D  mov        dword ptr [ecx],eax

Therefore, if we can determine that the expression does not contain elements with side effects (such as the function f), we should try to use the composite value assignment operator to facilitate writing at the same time without losing the efficiency.

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.