A macro in C will call a function that returns an integer and then returns a string

I have a function that returns an integer value. Now I want to write a macro that calls this function, gets the return value and adds a string to it and returns the resulting string.

I tried this:

#define TEST(x) is_enabled(x) 

I call this macro in the main function as follows:

 int ret = 0; ret = TEST(2); printf("PORT-%d\n", ret); 

It works great. However, I want the macro to return the string PORT-x, where x is the return value of the function being called. How can i do this?

EDIT:

I also tried writing it to several lines:

 #define TEST(x)\ {\ is_enabled(x);\ } 

And called it in the main function as follows:

 printf("PORT-%d\n", TEST(2)); 

But this gives a compile-time error:

 error: expected expression before Γ’{Γ’ token 
+5
source share
2 answers

Use a function, not a macro. There is no good reason to use a macro here.

You can solve it using sprintf(3) , in conjunction with malloc or a buffer. For more information, see Creating String C Strings (Do Not Print Them) or Help Pages.

About your editing: you don’t need to use curly braces {} in the macro, and they cause your error, because the preprocessing will translate it to something like

 printf("format%d", { is_enabled(x); }); 

To better understand macros, run gcc or clang with the -E flag or try reading this article: http://en.wikipedia.org/wiki/C_preprocessor

+5
source

This is a bit of a pain because you need to provide storage for the string. In truth, macros can now be reserved only for conditional compilation.

Constants are best done with enumerated types, and macro functions are usually better than built-in functions (provided that inline is a proposal to the compiler, not a requirement).

If you insist on using a macro, the repository can be executed with static repository, although it has problems with threads, if you use them, and delay / reuse of the returned string.

You can also dynamically allocate a line, but then you need to free it when you are done, and handle the lack of memory.

Perhaps the easiest way is to require the macro user to provide their own repository according to:

 #include <stdio.h> #define TEST2_STR(b,p) (sprintf(b,"PORT-%d",p),b) int main (void) { char buff[20]; puts (TEST2_STR(buff, 42)); return 0; } 

which outputs:

 PORT-42 

In case the macro seems a bit confusing, it uses a comma operator in which the expression (a, b) evaluates both a and b and has the result b .

In this case, it computes sprintf (which fills the buffer), then "returns" the buffer. And even if you think you have never seen this before, you are probably mistaken:

 for (i = 0, j = 9; i < 10; i++, j--) xyzzy[i] = plugh[j]; 

Despite the fact that most people think that the for function is a very different construct that can be used in many different places:

 int i, j, k; i = 7, j = 4, k = 42; while (puts("Hello, world"),sleep(1),1); 

(etc.).

+2
source

Source: https://habr.com/ru/post/1207640/


All Articles