The application of C-language variable parameters in macro definition

Source: Internet
Author: User

In the standard library of C, the input and output functions of the standard libraries of printf, scanf, sscanf, sprintf, sscanf, parameters are variable. When debugging a program, we may want to define a variable output function to record the log, so a macro with variable parameters is a good choice.

In C99 , a macro can also be defined as a function with variable parameters, such as:

#define LOG (format, ...) fprintf (stdout, format, __va_args__)
among them, ... Represents a mutable argument list, __va_args__ is replaced by the actual parameter set (argument list) in preprocessing.
GCC also supports a method with variable parameter names ( Note: VCs does not support):

#define LOG (format, args ...) fprintf (stdout, format, args)
Similarly, args is replaced by the actual set of parameters during the preprocessing process. Its usage is the same as above, except that the symbol of the parameter is changed.

It is important to note that the variable parameters of the above two methods cannot be omitted, although an empty parameter can be passed in. Speaking of which, it is necessary to mention the use of the "# #" Connection symbol, "# #" function is to connect token, the above example Format,args,__va_args can be regarded as token, if the token is empty, "# #" is not connected, so allow to omit variable parameters. Retrofit of the above 2 examples:

#define LOG (format, ...) fprintf (stdout, format, # #__VA_ARGS__) #define LOG (format, ARGS ...) fprintf (stdout, format, # # Args
If the parameters can be omitted, then a macro is used to define a switch, the function of implementing an output log is simple:

#ifdef debug#define LOG (format, ...) fprintf (stdout, ">>>>>" format "<<<<", # #__VA_ARGS__) # Else#define LOG (format, ...) #endif
In the development phase, a debug macro is added to the compilation options, which is where the log macro is used in the program, otherwise it simply calls an empty log macro, without any output. Assuming that the source file is TEST.C,GCC compile-time plus-ddebug (-D represents a predefined macro, which is defined by the compiler at the time of precompilation), the condition is established when the debug macro is judged, thus achieving the purpose of the output log:

Gcc-o Test Test.c-ddebug

Example: In the NDK development of Android, print the log macro:

You need to load the log module in the Android.mk file and add the Debug option:

Local_ldlibs: =-llog# Add Log module local_cflags+=-ddebug

NDKTest.cpp

 #include < jni.h> #include <android/log.h> #define LOG_TAG "HELLONDK" #ifdef debug#define logi (...) __android_log_print ( Android_log_info, Log_tag, __va_args__) #define LOGD (...) __android_log_print (Android_log_debug, Log_tag, __VA_ARGS__ ) #define LOGE (...) __android_log_print (Android_log_error, Log_tag, __va_args__) #else # define Logi (...) do{} while (0) # Define LOGD (...) do{} while (0) #define LOGE (...) do{} while (0) #endifJNIEXPORT jint jnicall jni_onload (javavm* vm, void* Res erved) {jnienv *env;jclass cls;if (vm->getenv ((void**) &env, jni_version_1_4)! = JNI_OK) {LOGE ("Init JAVAVM failed !"); return jni_err;} CLS = Env->findclass ("Com/learn/ndk/samplemodel"), if (CLS = = NULL) {LOGE ("can ' t find Com.learn.ndk.SampleModel."); return jni_err;} Class_com_learn_ndk_mainactivity = (jclass) env->newweakglobalref (CLS); Env->deletelocalref (CLS); return JNI_ version_1_4;} 

The application of C-language variable parameters in macro definition

Related Article

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.