473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

#define and external static

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 6590

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
"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
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
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
"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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ida | last post by:
Hi, I am trying to build an dll with Microsoft Visual C++ but during the linking phase I get linking errors. Script.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString...
2
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is...
7
by: Brian Sammon | last post by:
According to my programming textbook, "int a;" at the top level of a file is a definition which shouldn't go in a header file. However, I'm running into a lot of online documentation that treats...
3
by: al.cpwn | last post by:
do static and inline functions or members have internal linkage? I have been reading this newsgroup on google and found conflicting ideas. Can someone please help me understand why in some places...
1
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.