Use "#" and "#" in the macro definition of GCC Extension

Source: Internet
Author: User
"#" And "#" are used in macro definitions of GCC extensions "##"

The difference between "#" and "#" in macro definition is tested today.

The result is as follows:

"#" Indicates a connection to a string.

"##" Indicates a connection with a symbol. the symbol can be a variable or another macro symbol.

Example:

Macro definition:

(1)

# Define dev_file_name "/dev/test_kft"

# Define open_file (FD, N )/
{/
FD = open (dev_file_name # N, o_rdonly );/
If (FD <0 )/
{/
Printf ("open device error/N ");/
Return 0 ;/
}/
}

So called:

Open_file (fd1, 1 );
Open_file (fd2, 2 );
Open_file (FD3, 3 );
Open_file (fd4, 4 );
Open_file (fd5, 5 );
Open_file (fd6, 6 );

After expanding with GCC-E

2299: {fd1 = open ("/dev/test_kft", 00); If (fd1 <0) {printf ("open device error/N "); return 0 ;}};
2300: {fd2 = open ("/dev/test_kft" "2", 00); If (fd2 <0) {printf ("open device error/N "); return 0 ;}};
2301: {FD3 = open ("/dev/test_kft" "3", 00); If (FD3 <0) {printf ("open device error/N "); return 0 ;}};
2302: {fd4 = open ("/dev/test_kft" "4", 00); If (fd4 <0) {printf ("open device error/N "); return 0 ;}};
2303: {fd5 = open ("/dev/test_kft" "5", 00); If (fd5 <0) {printf ("open device error/N "); return 0 ;}};
2304: {fd6 = open ("/dev/test_kft" "6", 00); If (fd6 <0) {printf ("open device error/N "); return 0 ;}};

If dev_file_name is not defined

2299: {fd1 = open (dev_file_name "1", 00); If (fd1 <0) {printf ("open device error/N"); Return 0 ;}};
2300: {fd2 = open (dev_file_name "2", 00); If (fd2 <0) {printf ("open device error/N"); Return 0 ;}};
2301: {FD3 = open (dev_file_name "3", 00); If (FD3 <0) {printf ("open device error/N"); Return 0 ;}};
2302: {fd4 = open (dev_file_name "4", 00); If (fd4 <0) {printf ("open device error/N"); Return 0 ;}};
2303: {fd5 = open (dev_file_name "5", 00); If (fd5 <0) {printf ("open device error/N"); Return 0 ;}};
2304: {fd6 = open (dev_file_name "6", 00); If (fd6 <0) {printf ("open device error/N"); Return 0 ;}};

So we can clearly see that # N is parsed as "N", used to connect an existing string.

(2) Let's take a look at #. The macro definition is as follows:

# Define open_file (FD, N )/
{/
FD = open (dev_file_name ## N, o_rdonly );/
If (FD <0 )/
{/
Printf ("open device error/N ");/
Return 0 ;/
}/
}

The call method is the same.

Watch the macro development:

2299: {fd1 = open (dev_file_name1, 00); If (fd1 <0) {printf ("open device error/N"); Return 0 ;}};
2300: {fd2 = open (dev_file_name2, 00); If (fd2 <0) {printf ("open device error/N"); Return 0 ;}};
2301: {FD3 = open (dev_file_name3, 00); If (FD3 <0) {printf ("open device error/N"); Return 0 ;}};
2302: {fd4 = open (dev_file_name4, 00); If (fd4 <0) {printf ("open device error/N"); Return 0 ;}};
2303: {fd5 = open (dev_file_name5, 00); If (fd5 <0) {printf ("open device error/N"); Return 0 ;}};
2304: {fd6 = open (dev_file_name6, 00); If (fd6 <0) {printf ("open device error/N"); Return 0 ;}};

Now we can see that # N is used to directly connect n to the end of a symbol. Well, now let's define a symbol to see the effect.

# Define dev_file_name1 "/dev/test_kft1"

Expand again:

2299: {fd1 = open ("/dev/test_kft1", 00); If (fd1 <0) {printf ("open device error/N "); return 0 ;}};
2300: {fd2 = open (dev_file_name2, 00); If (fd2 <0) {printf ("open device error/N"); Return 0 ;}};
2301: {FD3 = open (dev_file_name3, 00); If (FD3 <0) {printf ("open device error/N"); Return 0 ;}};
2302: {fd4 = open (dev_file_name4, 00); If (fd4 <0) {printf ("open device error/N"); Return 0 ;}};
2303: {fd5 = open (dev_file_name5, 00); If (fd5 <0) {printf ("open device error/N"); Return 0 ;}};
2304: {fd6 = open (dev_file_name6, 00); If (fd6 <0) {printf ("open device error/N"); Return 0 ;}};

Obviously, the first symbol is replaced because it is a macro. In this way, we can also apply this extension feature to variables.

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.