I am trying to do this
#define _TEST_ test
#include <iostream>
int main()
{
std::cout << "_TEST_" << std::endl;
}
As far as my understanding, I expect this output.
test
However, the output I get is
_TEST_
Why am I doing wrong here?
"_TEST_"
is a string literal and not a macro. So no macro replacement will be done due to "_TEST_"
. To achieve your expected output you need to remove the surrounding double quotes and also change the macro to as shown below
//-------------vvvvvv--->double quotes added here
#define _TEST_ "test"
#include <iostream>
int main()
{
//-------------------vvvvvv---------------> not withing quotes
std::cout << _TEST_ << std::endl;
}
The output of the above modified program is:
test
In the modified program, the macro _TEST_
stands for the string literal "test"
. And thus when we use that macro in the statement std::cout << _TEST_ << std::endl;
, it will be replaced by the string literal, producing the expected output.