directory
- A. __VA_ARGS__ profile
- 2. __VA_ARGS__ use
- 1. The __VA_ARGS__ macro outputs string constants
- 2. The __VA_ARGS__ macro outputs variable arguments
- 3. __VA_ARGS__ shortcomings
- 1. Only string constants are supported, not mutable parameters
- 2. Only mutable parameters are supported, not string constants
- Four. Guess you like it
C/C++ learning Directory >> C language basics
A. __VA_ARGS__ profile
__VA_ARGS__ is a variable-parameter macro that was added to the new C99 specification and currently seems to be supported only by GCC (VC has been supporting it since VC2005).
[__VA_ARGS__] (https://www.codersrc.com/archives/9450.html) should be used with the define, in general is to the left in the macro.. Is copied exactly where __VA_ARGS__ is on the right; Here’s an example:
#define myprintf(...) printf( __VA_ARGS__)
Copy the code
Example code is as follows:
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language __VA_ARGS__ // @time :2021/07/10 08:00 //@Motto: There is no such thing as a thousand miles without small steps, no small streams without rivers and oceans, the wonderful program life needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #define myprintf(...) printf( __VA_ARGS__) int main() { myprintf("0123456789\n"); myprintf("www.codersrc.com\n"); Myprintf ("C language /C++ tutorial - ape say programming \n"); return 0; } /* 0123456789 www.codersrc.com C language /C++ tutorial - Ape say programming */Copy the code
Note:[__VA_ARGS__](https://www.codersrc.com/archives/9450.html)
It has to be something that doesn’t have any variablesstringConstants. if__VA_ARGS__
Contains variables, all of themprintf
The output of and variables cannot correspond one by one, and the output will be wrong.
2. __VA_ARGS__ use
__DATE__ The current date, a string constant in the format “MMM DD YYYY”.
__TIME__ The current time, a string constant in HH:MM:SS format.
__FILE__ This will contain the current file name, a string constant.
__LINE__ This will contain the current line number, a decimal constant.
__STDC__ is defined as 1 when the compiler compiles to ANSI standards; Determine if the file is standard C procedure.
The name of the function used by the precompiler when a __FUNCTION__ program is precompiled returns a string;
1. The __VA_ARGS__ macro outputs string constants
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language __VA_ARGS__ // @time :2021/07/10 08:00 //@Motto: There is no such thing as a thousand miles without small steps, no small streams without rivers and oceans, the wonderful program life needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #define LOGFUNC(...) (printf(__VA_ARGS__" - %d - %s/%s\n",__LINE__,__TIME__,__DATE__)) int main() { LOGFUNC("0123456789"); LOGFUNC("www.codersrc.com"); LOGFUNC("C /C++ tutorial - Ape say programming "); return 0; } /* 0123456789-9-07:52:40 /Jul 11 2021 www.codersrc.com - 10-07:52:40 /Jul 11 2021 C /C++ tutorial - Monkey Say programming - 11 - 07:52:40/Jul 11 2021 */Copy the code
Disadvantages:
-
Only strings are supported. Variable parameters or multiple parameters are not supported.
[__VA_ARGS__](https://www.codersrc.com/archives/9450.html)
Can only be string constants without any variables. -
if
__VA_ARGS__
Contains variables, all of themprintf
Output and variable can not correspond one by one, the output will be wrong;/**********/ // @author: Ape say programming // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial – C language VA_ARGS // @time :2021/07/10 08:00 //@Motto: No small steps no thousands of miles, no small streams no rivers and oceans, the wonderful life of the program needs unremitting accumulation! / * * * * * * * * * * /
#include <stdio.h> #define LOGFUNC(...) (printf(**VA_ARGS**" - %d - %s/%s\n",**LINE**,**TIME**,**DATE**)) int main() { LOGFUNC("0123456789"); LOGFUNC("%d,%d",1,2); LOGFUNC("%d,%d",1,2); Return 0; } / \ * main CPP: In function 'int main ()' : main. CPP: error: expected ') before the string constant 4 | # define LOGFUNC (...). (printf(**VA_ARGS**" - %d - %s/%s\n",**LINE**,**TIME**,**DATE**)) \*/Copy the code
2. The __VA_ARGS__ macro outputs variable arguments
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language __VA_ARGS__ // @time :2021/07/10 08:00 //@Motto: There is no such thing as a thousand miles without small steps, no small streams without rivers and oceans, the wonderful program life needs unremitting accumulation! /******************************************************************************************/ #include <stdio.h> #define LOGSTRINGS(fm, ...) __VA_ARGS__ printf (FM) int main () {/ / output variable parameter LOGSTRINGS (" 0123456789, "% d % s, 1," sd "); //OK // output string constant error //LOGSTRINGS("C language /C++ course - programming "); /*LOGSTRINGS("C language /C++ tutorial - "); Main.cpp: In function 'int main()' : main.cpp: error: Expected primary - expression before the ') 'token | 3 # define LOGSTRINGS (FM,...). printf(fm,__VA_ARGS__) */ return 0; } / * 0123456789, 1 sd * /Copy the code
3. __VA_ARGS__ shortcomings
1. Only string constants are supported, not mutable parameters
#define LOGFUNC(...) (printf(__VA_ARGS__))
Copy the code
2. Only mutable parameters are supported, not string constants
#define LOGSTRINGS(fm, ...) printf(fm,__VA_ARGS__)
Copy the code
So how can it be solved__VA_ARGS__
Support for both regular strings and mutable argument problems, which we’ll leave to the next article# #VA_ARGSTo achieve!
Four. Guess you like it
- C array subscript out of bounds and memory overflow difference
- C language pointer declaration and definition
- P ++ / p –
- The C languagep++/§ + + / _ (p++) / _p + +
- C language uses Pointers to iterate over groups of numbers
- C language pointer and array difference
- C language pointer array and array pointer difference
- C NULL pointer
- C void Pointers
- C language field pointer
- C function value passing and address passing
- Default parameter of C language function
- C language function variable parameter
- C function pointer
- C language pointer function
- C language callback function callback
- C typedef
- C defines constants
- C define prevents repeated inclusion of header files
- Pragma once
- C language #include <> is different from #include
- C const decorates a variable
- C const decorates Pointers
- C const modifier functions
- C const decorates function arguments
- C const is different from define
- C # operator
- C language ## operator
- C language __VA_ARGS__
C language __VA_ARGS__
This article is published by the blog – Ape Say Programming Ape Say programming!