473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

#define macro with multiple parrams

4
Hi,
I'm trying to define a macro that calls a log function:

#define LOG(level, device, pcString, args ...) if (level < SERIAL_LOG_LEVEL) _log(device, pcString, ## args)

void _log (unsigned char device, const char *pcString, ...);

I want the macro to accept multiple args with "..." but the compiler didn't like it. It works fine if i simply do.

#define LOG(level, device, pcString, args ) if (level < SERIAL_LOG_LEVEL) _log(device, pcString, ## args)

and when i call

d = 0;

LOG(1, 0, "hello world %d", d);

What am i doing wrong?

Thank you for your help.

Regards,

John
Aug 12 '08 #1
6 2682
gpraghuram
1,275 Expert 1GB
I think you miss the, after the args in the #define.

Raghu
Aug 12 '08 #2
JohnZ
4
I think you miss the, after the args in the #define.

Raghu
Ok, that works with 1 or more arguments, but if i do

LOG(1, 0, "hello world");

compiler says too few argument in macro.

I'm using IAR by the way.

Thank you.
Aug 12 '08 #3
gpraghuram
1,275 Expert 1GB
Ok, that works with 1 or more arguments, but if i do

LOG(1, 0, "hello world");

compiler says too few argument in macro.

I'm using IAR by the way.

Thank you.

Since you have specified 4 args followed by elipsis it is expecting 4 arguments atleast.
Then you shuld change the Macro....


Raghu
Aug 12 '08 #4
JohnZ
4
Since you have specified 4 args followed by elipsis it is expecting 4 arguments atleast.
Then you shuld change the Macro....


Raghu
Shouldn't the "..." allow 0 or more arguments? so that the below should accept 3 arguments or more.

#define LOG(level, device, pcString, ...) \
if (level < SERIAL_LOG_LEVEL) _log(device, pcString, ## __VA_ARGS__)

Shouldn't it?

Thank you.
Aug 12 '08 #5
JosAH
11,448 Expert 8TB
Shouldn't the "..." allow 0 or more arguments? so that the below should accept 3 arguments or more.

#define LOG(level, device, pcString, ...) \
if (level < SERIAL_LOG_LEVEL) _log(device, pcString, ## __VA_ARGS__)

Shouldn't it?

Thank you.
This is what the C99 Standard says about it:

[#12] If there is a ... in the identifier-list in the macro
definition, then the trailing arguments, including any
separating comma preprocessing tokens, are merged to form a
single item: the variable arguments. The number of arguments
so combined is such that, following merger, the number of
arguments is one more than the number of parameters in the
macro definition (excluding the ...).
kind regards,

Jos
Aug 12 '08 #6
JohnZ
4
This is what the C99 Standard says about it:



kind regards,

Jos
Thank you very much, I understand what I'm doing wrong now.
Aug 13 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
3
by: Bolin | last post by:
I am using a macro to define template functions that look the same except for a type. However when the return type it a template class with several template arguments (say, A<T1, T2>) then the...
18
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish...
34
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. ...
13
by: marcwentink | last post by:
Dear people, The code below is compiling: #define BUF_SZ 16383 ..... strcat(ConnectString, "Content-Length: BUF_SZ\n"); but it does not work since it give me:
4
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
14
by: raghu | last post by:
Hello I have a doubt plz clarify that #ifndef SYSTEM_H #define SYSTEM_H what will be the value of SYATEM_H after this #define statement and before that statement. Thanking you all Bye
4
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.