[C Language] (* p) ++ and * p ++ and ++ * p open a fog

Source: Internet
Author: User

Environment: win7

IDE: DEV-C ++

Compiler: GCC

 

1. Let's talk about the basics of ++ I and I ++.

The Code is as follows:

#include <stdio.h>//just change simplevoid stop(void){  system("pause"); }int main(void){ int i = 1; printf("i++ = %d\n",i++); printf("i = %d\n",i);  int j = 1; printf("++j = %d\n",++j); printf("j = %d\n",j);  printf("i++ = : %d   ++i = %d\n",i++,++i); printf("i = %d\n",i);  printf("++j = : %d   j++ = %d\n",++j,j++); printf("j = %d\n",j);  stop(); return 0;}

Running result:

 i++ =  i =  ++j =  j =  i++ = :    ++i =  i =  ++j = :    j++ =  j = 

1) only when I ++ encounters a semicolon (;) will it affect the value of I. The output value of I ++ is still 1, and the value of I is also 1.

2) After I passes the semicolon, I = 2 because of auto-incrementing

3) ++ j will affect the j value and ++ j value regardless of the Semicolon ";", so J = 2

4) The value of j is also 2.

5) depending on the compiler and the operating system, the printf computing direction is also different. This is calculated from the right, first ++ I, then I ++, so output 3 and 3.

6) The last I ++ encountered a semicolon, So I = 4

7) from the right, the result printed by j is still 2, but the value obtained by the second operation is 3, so ++ j = 4

 

2. Clarify the priority of * And ++ in * p ++.

 #include <stdio.h>    stop(        system(    main(        i =        *      p = &      printf(           v = *p++      printf(      printf(      printf(      system(        }

Running result:

 -p =  v =  -p =  i = 

From the result, the ++ symbol affects the value of p and does not affect the value of I. It seems that the priority of ++ is higher than the pointer * symbol.

 

Let's look at the example.

 #include <stdio.h>    stop(        system(    main(        i =        *      p = &      printf(           v = ++*      printf(      printf(      printf(      system(        }

Calculation Result:

 -p =  v =  -p =  i = 

How can ++ not affect the value of p?

 

Let's look at the example.

Slightly adjust the code: v = * ++ p;

Calculation Result:

 -p =  v =  -p =  i = 

Ah, it turns out that * And ++ have the same priority level. The same level is calculated from right to left.

 

OK. All problems are solved gradually,

* (P ++)

* (++ P)

(* P) ++

++ (* P) should be okay.

Fog finally opened up...

 

 

Related Article

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.