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 themprintfThe 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:

  1. 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.

  2. if__VA_ARGS__Contains variables, all of themprintfOutput 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

  1. C array subscript out of bounds and memory overflow difference
  2. C language pointer declaration and definition
  3. P ++ / p –
  4. The C languagep++/§ + + / _ (p++) / _p + +
  5. C language uses Pointers to iterate over groups of numbers
  6. C language pointer and array difference
  7. C language pointer array and array pointer difference
  8. C NULL pointer
  9. C void Pointers
  10. C language field pointer
  11. C function value passing and address passing
  12. Default parameter of C language function
  13. C language function variable parameter
  14. C function pointer
  15. C language pointer function
  16. C language callback function callback
  17. C typedef
  18. C defines constants
  19. C define prevents repeated inclusion of header files
  20. Pragma once
  21. C language #include <> is different from #include
  22. C const decorates a variable
  23. C const decorates Pointers
  24. C const modifier functions
  25. C const decorates function arguments
  26. C const is different from define
  27. C # operator
  28. C language ## operator
  29. C language __VA_ARGS__

C language __VA_ARGS__

This article is published by the blog – Ape Say Programming Ape Say programming!