449,035 Members | 1,064 Online
Need help? Post your question and get tips & solutions from a community of 449,035 IT Pros & Developers. It's quick & easy.

# #define and external static

 P: n/a Hi group, This is a follow-up of the question I posted this afternoon. I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI; If they are not, why? Thanks! Regards, Fan Nov 14 '05 #1
8 Replies

 P: n/a On Tue, 12 Oct 2004, Fan Zhang wrote: I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI; They are not. For a simple example of one major difference, try compiling and running int main(void) { printf("%g\n", 1/radian); return 0; } with the two different definitions. If you're using a C compiler, one of them won't even compile at all! (Read the FAQ.) If you try it with a C++ compiler (comp.lang.c++ is down the hall), you'll find that you get two different answers. (Read the FAQ.) HTH, -Arthur Nov 14 '05 #2

 P: n/a "Fan Zhang" writes: I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI; No. The former definition of radian is dangerous, because writing x/radian will not have the intended effect. The latter definitions cannot be used in constant expressions, and their values can change. (Even if you make them `const', they still cannot be used in constant expressions.) -- "I don't have C&V for that handy, but I've got Dan Pop." --E. Gibbons Nov 14 '05 #3

 P: n/a Fan Zhang wrote: Hi group, This is a follow-up of the question I posted this afternoon. I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI; If they are not, why? They are not. The latter two create actual objects in memory, which in turn must be initialized with compile time constants, which the initialization of radian is not (and should give a compile time error). -- Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net) Available for consulting/temporary embedded and systems. USE worldnet address! Nov 14 '05 #4

 P: n/a Thanks for everybody's input. This is what I found out last night after playing with it at home. It is not a good way to write #define radian 180./PI, although it is legal in my compiler (DevC++). The problem with it is #define does not do the calculation, it just does the REPLACEMENT! That is to say, when I tried to calculate 1./radian, it did 1./180./PI instead of 1./(180./PI). That is why the result did not make sense in my program, at least it appears to be. :-) Thanks again! "Fan Zhang" wrote in message news:ck***********@netnews.upenn.edu... Hi group, This is a follow-up of the question I posted this afternoon. I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI; If they are not, why? Thanks! Regards, Fan Nov 14 '05 #5

 P: n/a "Fan Zhang" writes: It is not a good way to write #define radian 180./PI, although it is legal in my compiler (DevC++). The problem with it is #define does not do the calculation, it just does the REPLACEMENT! That is to say, when I tried to calculate 1./radian, it did 1./180./PI instead of 1./(180./PI). That is why the result did not make sense in my program, at least it appears to be. :-) You can fix the problem with parentheses: #define radian (180./PI) -- "Given that computing power increases exponentially with time, algorithms with exponential or better O-notations are actually linear with a large constant." --Mike Lee Nov 14 '05 #6

 P: n/a "Fan Zhang" wrote in message news:... Hi group, This is a follow-up of the question I posted this afternoon. I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI I wonder whether they are the same as static double PI=3.1415926; static double radian=180./PI; If they are not, why? Thanks! Regards, Fan They are not equivalent. Preprocessor macros do not occupy memory the way variables do, and they do not obey the same scope and visibility rules as variables. Preprocessor macros are simply text strings that are replaced in the source before it is actually compiled. And your second example won't compile as written. Initializers for static or extern variables must be compile-time constants, which 180./PI isn't. Nov 14 '05 #7