Home > Backend Development > C#.Net Tutorial > What is the difference between a++ and ++a in C language

What is the difference between a++ and ++a in C language

王林
Release: 2020-07-11 09:45:20
Original
26455 people have browsed it

The difference between a and a in C language is: the operation process of the two is different. a means to use the value of a first, and then add 1 to a; a means to add 1 to a first, Then use the value of a again. Both a and a are equivalent to [a = a 1].

What is the difference between a++ and ++a in C language

Auto-increment operator:

is an auto-increment operator: such as a, a is equivalent to a = a 1;

So what is the difference between a and a?

Although the equivalent results of a and a are the same, the operation processes are different. a means to use the value of a first, and then add 1 to a; a means to add 1 to a first, and then use the value of a.

Let’s take a look at an example:

    #include <stdio.h>  
    int main()  
    {  
        //int m = 10, n1, n2;  
        //n1 = m++;先将m的值赋给n1,然后m再做自增运算,所以此时,n1=10,m=11;  
        //n2 = ++m ;先将m做自增运算,然后在将运算后的m值赋给n2,所以此时,n2=11,m=11;  
      
        int a = 10,b =10, c, d;  
        c = (a++) + (++a);  
        //由上例n1=m++,n2=++m,m++=11可得出c=10+12;分析:前面括号所得值为10,而前面括号中的a经过自增运算后a的值为11,然后赋值给后面括号中的a,后面括号中的a经过自增运算后a的值为12,后面括号最后赋值为12;  
        d = (++b) + (b++);  
        //由上例n1=m++,n2=++m,++m=11可得出d=11+11;分析:前面括号所得值为11,而前面括号中的a经过自增运算后b的值为11,然后赋值给后面括号中的b,后面括号中的b先将值赋给后面括号,所以后面括号的值为11;  
        printf("c=%d\nd=%d\n",c,d);  
        return 0;  
    }
Copy after login

The above is the detailed content of What is the difference between a++ and ++a in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template