C language mutable parameters used in macro definitions

Source: Internet
Author: User

In the C standard library language, printf, scanf, sscanf, sprintf, sscanf into the output function, the parameters are variable. When you debug a program. We may want to define a variable output function to record the log, so a variable-parameter macro is a good choice.

In C99 , macros can also be defined as functions with variable parameters, such as:

#define LOG (format, ...) fprintf (stdout, format, __va_args__)
among them, ... Represents a variable parameter list, which is replaced by the actual set of arguments ( the __va_args__) in preprocessing.
at the same time, GCC also supports the ability to change the number of names ( Note: VCs does not support):

#define LOG (format, args ...) fprintf (stdout, format, args)
In the same way, the args are replaced by the actual set of parameters during the preprocessing process. It is used in the same way as above, and only the symbol of the parameter changes.

It is important to note that the variable parameters of the two methods can not be omitted, although it is possible to pass an empty parameter in. Here, 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 seen as tokens. Assuming that token is empty, "# #" is not connected. So agree to omit the variable number of parameters.

Retrofit of the above 2 demo examples:

#define LOG (format, ...) fprintf (stdout, format, # #__VA_ARGS__) #define LOG (format, ARGS ...) fprintf (stdout, format, # # Args
If the number of 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, add a debug macro to the compilation option, which will output the log where log macros are used in the program. Otherwise, it just calls an empty log macro, no matter what the output is. If the source file is TEST.C,GCC compiled with-ddebug (-D for predefined macros, defined by the compiler at the time of precompilation), the condition is set when the debug macro is inferred. To achieve the purpose of the output log:

Gcc-o Test Test.c-ddebug

Demo Sample: In the NDK development of Android. To print a macro for a log:

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

Local_ldlibs: =-llog# Join the 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;} 

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

C language mutable parameters used in macro definitions

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.