-5

I have to write a macro function which takes multiple argument and print them. e.g,

int main(){
    int x,y;
    x=5;
    y=9;
    cout<<"value of x is::"<<x<<" "<<"value of y is:::"<<y<<endl;
    return 0;
}

I want to write a macro which takes x and y as argument and print same as above. like debug_print(x,y).

  • 6
    So, what's stopping you from writing said macro? Note: why macro instead of a function? – Algirdas Preidžius Mar 21 '18 at 14:44
  • 1
    My general advice is: Don't do that. Avoid macros as much as you can (and then avoid them some more). However, if that's not possible then you have to tell us what you have tried, and how id worked (or not worked). And if you know how to create single-argument macros, then it should not be hard to extrapolate and at least *guess* how a two-argument macro should be. – Some programmer dude Mar 21 '18 at 14:45
  • what's your problem? You clearly already know the Syntax for multi-argument Macros, what problem did you run into? – Zinki Mar 21 '18 at 14:45
  • 1
    Don't do that. Just [write C++](https://stackoverflow.com/a/29326784/1362568) – Mike Kinghan Mar 21 '18 at 14:47
  • I wrote the macro function which is #define debug_print(x,y)(cout<<"x is::< – Anamika Kumari Mar 21 '18 at 14:48
  • Can you put all that in your question and put the error message in as well please? – Galik Mar 21 '18 at 14:53
  • Take care where you put your quotes. And please edit your question to have a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) of the non-working code. Then while editing your question, please copy-paste the full and complete output of the compiler into the body of the question, if there is any of course. And *tell* us *how* "it is not working". – Some programmer dude Mar 21 '18 at 14:53

1 Answers1

0

What you want to look into are variadic macros if you want a variadic number of parameters. Using those, you can define a macro like this

#define debug_print(...) <your code here>

in the <your code here> section you can then use __VA_ARGS__.


If you only want to use two parameters, you can define the macro like this:

#define debug_print(x, y) (std::cout << "value of x is::" << x << " " << "value of y is:::" << y << std::endl)

and use it like

int x = 5;
int y = 9;
debug_print(x, y);

As the others have already said, please do not use this macro unless you absolutely must! E.g. you don't see that debug_print needs iostream to work, and you don't see which types can be passed into it.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • 2
    *"in the `` section you can then use `__VA_ARGS__`"* - A bit of a cop out on your part if I may say so. It's not at all obvious how to complete ``, especially to a novice. – StoryTeller - Unslander Monica Mar 21 '18 at 14:58
  • 1
    @StoryTeller you are very right on that point; it's just that 1. I haven't really worked with variadic arguments myself, so instead of putting potentially wrong code, I just linked to a tutorial and 2. the `` part depends heavily on what the OP wants to do, especially because it seems like they only want 2 parameters, so variadic arguments are overkill anyways. Thank you for your feedback :) – Thomas Flinkow Mar 21 '18 at 15:01
  • Thanks for the solution. It is working – Anamika Kumari Mar 21 '18 at 15:05
  • @AnamikaKumari glad to hear that it's working for you! – Thomas Flinkow Mar 21 '18 at 15:06