#define and external static

F

Fan Zhang

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
 
A

Arthur J. O'Dwyer

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
 
B

Ben Pfaff

Fan Zhang said:
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.)
 
C

CBFalconer

Fan said:
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).
 
F

Fan Zhang

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!
 
B

Ben Pfaff

Fan Zhang said:
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)
 
J

John Bode

Fan Zhang said:
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.
 
C

CBFalconer

Fan said:
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.
:)

That is why #defines are normally guarded by parentheses, as in:

#define radian (180/PI)

Please don't toppost. Your answer belongs after, or intermixed
with, the material to which you reply after snipping anything that
is not germane to your reply. Topposting is considered rude and
boorish in the better newsgroups.
 
P

pete

CBFalconer said:
That is why #defines are normally guarded by parentheses, as in:

#define radian (180/PI)

For me, it's always been simpler to consider the word "radian"
as meaningless in mathematic expressions.
From there, it follows that the symbol for degrees
is a numeric constant.

/* BEGIN new.c */

#include <stdio.h>
#include <math.h>

#define pi 3.14159265358979
#define degrees (pi / 180)

int main(void)
{
printf("The sine of 90 degrees is %f\n", sin(90 * degrees));
return 0;
}

/* END new.c */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top