definition of constants that i want to use in the programs

#define pi=3.14
Last edited on
closed account (yUq2Nwbp)
#define MAX 8

....where you write MAX compiler will understand it 8..
#define is a pre-processor directive, that replaces all occurrences of a specified identifier (in your case pi) with a value (which I assume for you is 3.14), it is not a variable definition in anyway.

You need to remove the = sign from your #define pi=3.14 statement as the pre-processor will not understand it.

The format of the #define directive is as follows:

#define IDENTIFIER value , in your case it would be
#define PI 3.14 , notice there is a space between PI and 3.14. Also there is a convention that identifiers for constants should be written in capitals.

Here are some links that I hope will clear things up for you:

http://www.cppreference.com/wiki/preprocessor/replace
http://www.cplusplus.com/doc/tutorial/preprocessor/

I hope this helps!
Try not to use #define too much though. in the above case.. if i were to write hi_PI, the processor would translate it to hi_3.14. so that could be a problem sometimes..

A better aproach would be using a namespace, like so.

(outside of the main loop)

1
2
3
4
5
namespace {

  const float pi = 3.14;

}


hope this helps and good luck!
Last edited on
Or just use a constant like:
 
const double PI = 3.14;
Last edited on
Another option if you are only using this in a specific area of your program is to use:
1
2
3
4
5
#define PI 3.14

 ***Code that uses PI***

#undef PI 


This can be pretty handy sometimes.
if i were to write hi_PI, the processor would translate it to hi_3.14
No, it would not. It is case sensitive and it would not parse "half symbol"

Besides there is already M_PI in cmath
oh, didn't know that :D the previous post stated

 
#define PI 3.14 
Topic archived. No new replies allowed.