#define in C++

Ty4 clicking

Okay, in a book I read that the syntax to the symbolic constant declarations goes something like this :

#define NAME CONSTANT

and it was also written that where ever the compiler will come across 'NAME' it will be substituted by the sequence of characters 'CONSTANT'

But then in an example to use PI as 3.14 they had used

#define PI 3.14

Is this correct ?
I mean, 3.14 is an floating type so we are actually expecting the compiler to substitute an FLOATING TYPE everywhere the word PI is encountered,
and according to my knowledge the #define keyword only substitutes CHARACTER

Help me clear the confusion
I think you're a bit confused about the workings of the preprocessor.

All the preprocessor does is a context-insensitive text replace. When you say #define PI 3.14, you're telling the preprocessor to find all instances of the text "PI" and replace it with the text "3.14". Whether this makes sense or not is a different matter.
I think if there's something like "calculatePI", it will not be replaced with "calculate3.14", though.
#defining constants or macros is not really needed on C++.
I think the best thing for the example provided is using const float PI = 3.14;
If you want more documentation of the prepocessor, read http://www.cplusplus.com/doc/tutorial/preprocessor.html
Topic archived. No new replies allowed.