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 belowThe output of the above modified program is:
Demo
Explanation
In the modified program, the macro
_TEST_
stands for the string literal"test"
. And thus when we use that macro in the statementstd::cout << _TEST_ << std::endl;
, it will be replaced by the string literal, producing the expected output.