473,668 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 52358

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*********@ya hoo.com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
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,v al))
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,v al))

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*********@yah oo.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
16289
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 up space, is there any reason to choose it over a #define'd constant? -- Thank you P.S. if it makes any difference, I ssh to a SunOs machine where I use
9
3089
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" If I change NUMBER1 and NUMBER2 I must change STRING1 and STRING2 consequently.
13
2041
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
2316
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) so after that point whenever you use X it gets replaced by (*X)
19
2418
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 declared in a header may be additionally implemented as a macro defined in the header, so a library function should not be declared explicitly if its header is included". Is this applicable to standard functions? And, what if the definition
4
2430
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 c1_syn, u64 c2_syn, u64 c_stat, u64 c_sts, int print) { char *sourcename = { "UNKNOWN", "UNKNOWN", "UNKNOWN", "MEMORY", "BCACHE", "DCACHE",
6
27837
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
2435
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
2779
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 i am not getting any errors .
23
3905
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
8371
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8889
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8572
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8652
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.