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 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
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 :-(
"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;
}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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"...
|
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:
|
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)
...
|
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...
|
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...
|
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 \
... \
... \
|
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
|
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...
|
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;
};
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |