By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,035 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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" <fa******@sas.upenn.edu> 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.
<http://cbfalconer.home.att.net> 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" <fa******@sas.upenn.edu> 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" <fa******@sas.upenn.edu> 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" <fa******@sas.upenn.edu> 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

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

P: n/a
Fan Zhang wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

P: n/a
CBFalconer wrote:

Fan Zhang wrote:

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)


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 */
--
pete
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.