-1

I write #define macros

I want to have a function or a macro that prints its name when I give it a value

for example :

`

#define ten 10

 string printName(int value);
int main()
{
    cout<<printName(10);
}

`

output : ten

A solution or code sample

Omid_Dev
  • 21
  • 1
  • What name? Do you mean print the number using words? Well, what have you tried so far? This is not code-writing service. – Quimby Nov 01 '22 at 06:17
  • Give me the number of macros and return the name to me – Omid_Dev Nov 01 '22 at 06:20
  • Okay, and what did you try so far? What problems are you having with your solution? – Quimby Nov 01 '22 at 06:22
  • The general rule for C++ is NOT to use since macros are not typesafe (unless you have to). In this case `static constexpr int ten{10};'` would be a better approach to define a constant. Side note, stop using [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Pepijn Kramer Nov 01 '22 at 06:35

2 Answers2

1

The pre processor doesn't allow what you are trying to do

Also consider:

#define ten 10
#define xxx 10

string printName(int value);
int main()
{
    cout<<printName(10);
}

What would you expect it to output ? Just because it makes sense to you doesn't mean it makes sense to the compiler / cpp.

John3136
  • 28,809
  • 4
  • 51
  • 69
0

Use of macros should be avoided wherever possible.

Instead you could use a std::map<int, std::string> for your purpose:

int main()
{
    const std::map<int, std::string> printName{{10, "ten"}, {11, "eleven"}};//add more if you want 
    
    std::cout << printName.at(10) << std::endl;  //prints ten
}
Jason
  • 36,170
  • 5
  • 26
  • 60