What is a pre-processor macro and how is it processed by the C++ system? Demonstrate a potential problem with this approach by referring to the macro

#define SQR(t) t*t

what is wrong here... besides the fact that if you put a type that cant use the * operator you'll get a compile time error

Recommended Answers

All 2 Replies

what is wrong here...

Side effects, my inquisitive friend:

int x = 2;
cout<< SQR(x++) <<'\n';
cout<< x <<'\n';

x is incremented twice due to the macro being nothing more than a textual substitution. Thus the expanded macro is x++ * x++ .

commented: perfect thank you +1

That is not a very easy macro definition to demonstrate the other error but consider this code using that macro ~SQR(2) . What do expect the output to be, what is it?

This is normally more easily demonstrated with the macro in question uses + rather than *.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.