; Preprocessor definitions

Why do i get a compilation error when executing the code line

<em>#define PI ;</em>

why don’t the preprocessor just replace every occurrence of PI with

<em>';'</em>

as mentioned here at www.cplusplus.com/doc/tutorial/constants in the last paragraph of the page.

1 Like

for Precessor directives there is no semicolon at the end

Hi,
I tried this out and it does work. The preprocessor indeed replaces every occurrence of ‘PI’ with a semi-colon. Have a look at

Can you provide some more details about the compilation error you encountered?

Abhinav

1 Like

@jr_coder Preprocessor JUST replaces the macro-name with the text given in the code. It doesn’t add anything else. it just copies and paste the text at the place of macro-name!

As told by @abhinav92003 it works fine if you use PI as ;

Could you show your whole code, then it will be easier to know the cause of error (if any error occurs)

Writing #define PI 3.14; don’t shows an error if you use it like I did here : http://ideone.com/AyFTDX
But if you add semi-colon after you have ended one statement, then it wont throw any error since just a ; (semi-colon) is a null statement! (Example : http://ideone.com/8znOp5)

But something like this can also happen : https://discuss.codechef.com/questions/88958/when-defining-macros-why-do-we-have-to-give-the-value-in-the-function

So it’s always better to use it like this (Where you don’t include ; (semi-colon) in the text used in defining the macro) : http://ideone.com/LUSZX7

@jr_coder I hope this helps :slight_smile:

Then why writing

<em>#define PI 3.14;

don’t shows an error?

Thanks for the example code.