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
ksri123
December 24, 2016, 5:35pm
2
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 : AyFTDX - Online C++0x Compiler & Debugging Tool - Ideone.com
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 : 8znOp5 - Online C++0x Compiler & Debugging Tool - Ideone.com )
But something like this can also happen : When defining macros, why do we have to give the value in the function? - general - CodeChef Discuss
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) : LUSZX7 - Online C++0x Compiler & Debugging Tool - Ideone.com
@jr_coder I hope this helps
Thanks for the example code.