#,## in C + +, and "

Source: Internet
Author: User

This Article home page links: #,## in C + +, and "


Want to apply macros flexibly, inseparable # from and ## .

"

learning # and ## , first look at a " Example:

#include <stdio.h>#include <string.h>int Main(){    Const Char* P1 = "Hello," "world!";     //A space    Const Char* P2 = "Hello,"    "world!";  //Multiple spaces    Const Char* P3 = " Hello, " "world!.";      //No spaces    Const Char* P4 = "hello,world!";        //A whole string    Const Char* P5 = " Nihao, " "shijie!.";  //A different string    printf("P1 =%s, strlen (p1) =%d\ n", P1, strlen(P1));    printf("P2 =%s, strlen (p2) =%d\ n", P2, strlen(P2));    printf("p3 =%s, strlen (p3) =%d\ n", P3, strlen(P3));    printf("P4 =%s, strlen (p4) =%d\ n", P4, strlen(P4));    printf("P5 =%s, strlen (p5) =%d\ n", P5, strlen(P5));    return 0;}

The output is:

P1 = hello,world!, strlen (p1) = 12p2 = hello,world!, strlen (p2) = 12P3 = hello,world!, Strle N (p3) = 12P4 = hello,world!, strlen (p4) = 12P5 = nihao,shijie!, strlen (p5) =  

View the constant string segment of a PE file and discover that only one string exists after the compiler has been optimized Hello,World! .

That is to say, p1,p2,p3,p4 these four kinds of writing is equivalent, this is the premise that explains # usage afterwards.

String Operation (#)

When used as a string operation, # the primary role is to convert macro parameters to string constants without being extended.
Points:

  • The space between the left and right sides of the macro definition parameter is ignored, and multiple spaces between tokens of the parameter are converted to a single space.
  • The
  • macro definition parameter contains the need for special meaning word such as " or \ , they are automatically preceded by an escape character \ .

or through the MSDN example to see easy to understand:

 #define F ABC   #define B def   #define FB (ARG) #arg  # Define FB1 (ARG) FB (ARG)  fb   ( f  b  )  fb1   ( f  b  )  

When I first saw that the two lines pre-compile the effect is the same, but see the use of GCC-E compiled code, it is understood that MSDN on the "no extension" has a deeper understanding of the actual pre-compiled code:

"F B";"abc def";

Presumably the transformation process should be as follows:

FB(F B) --> #F B -->"F B"  FB1(F B) --> FB1(abc def) --> FB(abc def) --> #abc def --> "abc def"
Tag Join operation (# #)

Connect multiple tokens to a token.
Points:

    • It cannot be the first or last token in a macro definition.
    • Before and after the space is optional.

To understand the examples on MSDN:

#include <stdio.h>#define Paster (n) printf_s ("token" #n "=%d", token# #n)int Token9 = 9;int Main(){   Paster(9);}

paster(9);The preprocessing steps should be as follows:

  • paster(9);
  • printf_s( "token" #9 " = %d", token##9 );
  • printf_s( "token" "9" " = %d", token9 );
  • printf_s( "token9 = %d", token9 );

This should be a good understanding.

Refer to MSDN
    • Stringizing Operator (#): http://msdn.microsoft.com/EN-US/library/7e3a913x (V=vs.110,d=hv.2). aspx
    • Token-pasting Operator (# #): Http://msdn.microsoft.com/EN-US/library/09dwwt6y (V=vs.110,d=hv.2). aspx

#,## in C + +, and "

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.