# // # Usage in macro definition

Source: Internet
Author: User

About # // # usage in macro definition () http://blog.chinaunix.net/space.php? Uid = 20792262 & Do = Blog & id = 201691 classification: algorithms and data structures

 

1st

In the macro definition of C/C ++, #/# has a special function. 1. # The operator converts a macro parameter to a string literal. It can only appear in the replacement list of macros with parameters.

View plaincopy to clipboardprint?
  1. # Include <cstdio>
  2. # Define print_str (s) printf (# s "= % s/n", S)
  3. Int main ()
  4. {
  5. Char * S1 = "AAA ";
  6. Print_str (S1 );
  7. Return 0;
  8. }

Output: S1 = aaa Note: printf (# s "= % s/n", S)-> printf (S1 "= % s/n", S1) -> printf ("S1 = % s/n", S1)-> printf ("S1 = % s/n", "AAA ") # S is only used to replace the macro parameter area. S is still a variable name. 2. # The operator can "stick" two marks (for example, identifiers) together to form a mark. (No need to be surprised. # The operator is called Mark bonding ".) If one of the operands is a macro parameter, "bonding" will occur after the formal parameter is replaced by the actual parameter.

View plaincopy to clipboardprint?
  1. # Include <cstdio>
  2. # Define print_str (I) printf ("str = % s/n", s # I)
  3. Int main ()
  4. {
  5. Char * S1 = "S1 ";
  6. Char * S2 = "S2 ";
  7. Char * S3 = "S3 ";
  8. Print_str (1 );
  9. Print_str (2 );
  10. Print_str (3 );
  11. Return 0;
  12. }

Output: Str = S1
STR = S2
STR = s33.c99 complex macro: Variable Parameter

View plaincopy to clipboardprint?
  1. # Include <cstdio>
  2. # Define print_str (format,...) do {printf (format, _ va_args _);} while (0)
  3. Int main ()
  4. {
  5. Char * str1 = "S1 ";
  6. Char * str2 = "S2 ";
  7. Print_str ("str1 is % S/nstr2 is % s/n", str1, str2 );
  8. Return 0;
  9. }

Output: str1 is S1
Str2 is s2but attention!

View plaincopy to clipboardprint?
  1. # Include <cstdio>
  2. # Define print_str (format,...) do {printf (format, _ va_args _);} while (0)
  3. Int main ()
  4. {
  5. Char * str1 = "S1 ";
  6. Char * str2 = "S2 ";
  7. Print_str ("str1 is over/N ");
  8. Return 0;
  9. }

Compile: macro1.cpp: In function 'int main ()':
Macro1.cpp: 8: Error: expected primary-expression before ') 'token
An error is prompted here because the variable parameter list is missing and only the format parameter is used. Here, a ',' is added after the format parameter ','. So, gnu cpp came up with a special # operation. That's right, # The operation is coming again, but it's not the above concatenation. If the variable parameter is ignored or empty, the "#" operation will remove the comma before the Preprocessor. If some variable parameters are provided during macro calls, gnu cpp will also work normally and put these variable parameters after the comma.

View plaincopy to clipboardprint?
  1. # Include <cstdio>
  2. # Define print_str (format,...) do {printf (format, ##__va_args _);} while (0)
  3. Int main ()
  4. {
  5. Char * str1 = "S1 ";
  6. Char * str2 = "S2 ";
  7. Print_str ("str1 is over/N ");
  8. Return 0;
  9. }

 

Output: str1 is over





2nd

In C/C ++, the macro definition is completed by define. There are three special symbols in define:

1. #: During macro expansion, the # parameter is replaced with a string, for example:

# Define P (exp) printf (# exp );

When P (asdfsadf) is called, # exp is replaced with "asdfsadf"

2. ##: splice the first and second words together. For example, in the C programming language:

# Define CAT (x, y) x # Y

Call CAT (VAR, 123) and expand to var123.

3. # @: change the value sequence to a character

# Define CH (c) # @ C

Call CH (A) and expand it to 'A '.

I wrote a short test program myself:

# Define a (a, B) a # B
# Define B (a) #
# Define C (a) # @

# Include <iostream>

Using namespace STD;

Void main ()
{
Int V = 0, V1 = 1, V12 = 2; // v stands for VAR
Cout <A (V1, 2) <Endl;
Cout <B (V1) <Endl;
Cout <C (v) <Endl;
}

Result:

1

V1

V

 

Copyright:Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held.

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.