Macro _ va_args _ of variable parameters in C Language __

Source: Internet
Author: User

Macro _ va_args _ of variable parameters in C Language __

 

1 .#

If you want to include macro parameters in a string, ansi c can do this. In the macro replacement section of the class function,

# The symbol is used as a preprocessing operator to convert the language symbol into a string.

 

For example, if X is a macro parameter, # X can convert the parameter name into a corresponding string. This process is called stringizing ).

# Incldue <stdio. h>

# Define psqr (x) printf ("the square of" # X "is % d. \ n", (x) * (x ))

 

Int main (void)

{

Int y = 4;

Psqr (y );

Psqr (2 + 4 );

Return 0;

}

Output result:

The square of Y is 16.

The square of 2 + 4 is 36.

Use "Y" instead of # X for the first macro call; Use "2 + 4" generation # X for the second macro call.

 

2 .##

# Use a class FUNCTION macro to replace the operator. In addition, ## can be used to replace Class Object macros.

This operator combines two language symbols into a single language symbol.

 

For example:

# Define xname (n) x # N

Macro call:

Xname (4)

After expansion:

X4

 

 

Program:

# Include <stdio. h>

# Define xname (n) x # N

# Define pxn (n) printf ("X" # N "= % d \ n", X # N)

 

Int main (void)

{

Int xname (1) = 12; // int X1 = 12;

Pxn (1); // printf ("X1 = % d \ n", X1 );

Return 0;

}

 

 

3. _ va_args __

_ Va_args _ can be used to replace the parameter list. The procedure is as follows:

 

# Include <stdio. h>

# Include <fstream>

# Include <iostream>

# Include <time. h>

# Include <string>

Using namespace STD;

Void savelog (char * cfilename, char * cformat ,...)

{

Char cbuffer [2048];

Memset (cbuffer, 0, 2048 );

 

Va_list argptr;

Va_start (argptr, cformat );

Vsprintf_s (cbuffer, 2048, cformat, argptr );

Va_end (argptr );

 

Time_t t = time (null );

Char * cdate = ctime (& T );

 

Ofstream file (cfilename, IOS: APP );

File. Write (cdate, strlen (cdate)-1 );

File <ends;

File <cbuffer <Endl;

File. Close ();

 

}

 

# Define enable_save_log

# Ifdef enable_save_log

# Define save_log (x,...) savelog ("test. log", "% s:" # X, _ FUNCTION __,
##__ Va_args __)

 

# Else

# Define save_log (x ,...)

# Endif

Here # removes the redundant ",". When the number of variable parameters is 0, it actually becomes save_log (X ).

 

1. _ va_args _ is a macro of a Variable Parameter. Few people know about this macro. The macro of this variable parameter is added in the new c99 specification,

Currently, only GCC is supported (not supported by the vc6.0 compiler ). When the number of variable parameters is 0,

# Here serves to remove unnecessary ",". Otherwise, compilation errors may occur. You can try it.

 

2. The _ file _ macro is replaced with the current source file name during pre-compilation.

 

3. _ line _ macro is replaced with the current row number during pre-compilation.

 

4. _ function _ macro is replaced with the current function name during pre-compilation.

 

 

 

 

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.