473,320 Members | 1,948 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,320 software developers and data experts.

How to define C macro

Hello All,

Here is a small C program,

main()
{
int a= 100;
float b =99.99;
TEST(a,%d);
TEST(b,%f);
}

Now I want to write a macro for TEST such that it outputs something like this

main()
{
int a=100;
float b =99.99;
printf(" The value of a = %d \n",a);
printf(" The value of b = %f \n",b);
}

I tried to write macro like this, but its not working

#define TEST(a,b) printf(" The value of a = b \n",a)

Can somebody help me in this?

Thanks
-Vittal
Nov 13 '05 #1
4 52335

On Thu, 3 Jul 2003, Vittal wrote:

TEST(a,%d);
TEST(b,%f);

Now I want to write a macro for TEST such that it outputs something like this

printf(" The value of a = %d \n",a);
printf(" The value of b = %f \n",b);

I tried to write macro like this, but its not working

#define TEST(a,b) printf(" The value of a = b \n",a)


Try

#define TEST(a,b) printf(" The value of " #a " = " #b " \n", a)

(The syntax #foo is a special preprocessing thingamabob that says
"take the value of foo and stick it in a string literal." Putting
two string literals next to each other - "foo" "bar" - concatenates
them - producing the equivalent of "foobar". [This *only* works with
compile-time literals!] So the above stringizes 'a' and 'b' and
sticks them in the string.)

Untested code, may not work if a or b are macros themselves. I.e.,

TEST(INT_MAX, %d);

may do incorrect things. Someone else will post that FAQ. :)

-Arthur

Nov 13 '05 #2
In article <f9**************************@posting.google.com >, Vittal wrote:
Hello All,
#define TEST(a,b) printf(" The value of a = b \n",a)


#define TEST(a,b) printf("The value of " #a " = " #b "\n", a)

Interresting question in fact.

Marc Boyer
--
Lying for having sex or lying for making war? Trust US presidents :-(
Nov 13 '05 #3
"Vittal" <vs*********@yahoo.com> wrote in message
news:f9**************************@posting.google.c om...
Hello All,

Here is a small C program,

main()
{
int a= 100;
float b =99.99;
TEST(a,%d);
TEST(b,%f);
}

Now I want to write a macro for TEST such that it outputs something like this
main()
{
int a=100;
float b =99.99;
printf(" The value of a = %d \n",a);
printf(" The value of b = %f \n",b);
}

I tried to write macro like this, but its not working

#define TEST(a,b) printf(" The value of a = b \n",a)

Can somebody help me in this?

Thanks
-Vittal


Hi Vittal,

You can use the define:
#define TEST(fmt,val) ((void)printf("The value of %s = "fmt"\n",#val,val))
The format(fmt) is just a string and is concatenated with the rest of the
strings.
#val is also a string (so "a" or "b" in your example)
val is the value.

Marco

#include <stdio.h>

#define TEST(fmt,val) ((void)printf("The value of %s = "fmt"\n",#val,val))

int main()
{
int a=100;
float b=99.99F;

TEST("%d",a);
TEST("%f",b);
return 0;
}
Nov 13 '05 #4
In <f9**************************@posting.google.com > vs*********@yahoo.com (Vittal) writes:
main()
{
int a= 100;
float b =99.99;
TEST(a,%d);
TEST(b,%f);
}

Now I want to write a macro for TEST such that it outputs something like this

main()
{
int a=100;
float b =99.99;
printf(" The value of a = %d \n",a);
printf(" The value of b = %f \n",b);
}

I tried to write macro like this, but its not working

#define TEST(a,b) printf(" The value of a = b \n",a)
Obviously, since the preprocessor doesn't touch the contents of string
literals.
Can somebody help me in this?


Use the # operator and take advantage of the adjacent string splicing
feature of C:

#define TEST(a,b) printf(" The value of " #a " = " #b " \n", a)

Not very easy to read, but it gets the job done.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Robert | last post by:
Greetings everyone, I was wondering if a const variable or object took up space. I know that a #define'd macro doesn't, as it's basically just interpreted by the compiler. If a const does take...
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
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:
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
19
by: Sensei | last post by:
Hi! I'm concerned about the legality of such a definition: #define funcX funcY where funcX belongs to the *standard* C functions. Is it legal to do this? The standard says "any function...
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...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.