-1

Which is better:

#define PI 3.14 

or

const double PI = 3.14; 

This was an interview question, and I don't seem to find an unambiguous answer.

I suppose I should prefer not to use macros, but I'd like to have a sturdy answer to this.

EDIT: So the question marked duplicate says, that define "don't respect scopes", but I don't really want scopes here, I want something like a super global, PI must be PI everywhere, should be accessed from anywhere. #define seems more reasonable here to me.

Also I don't see where could using PI go wrong in compile time so please provide an example.

1
  • id argue that it depends on your target platform. more specifically, the way the compiler creates the assembly and the underlying cpu instruction set. Just as a for instance, if you #define a constant double, and the compiler emits it as an immediate value, an instruction might become 2-4 bytes larger than if it simply emitted a reference to a memory location. 2-4 bytes 10000 times adds up to not a whole hell of a lot. BUT if you only have 4kb of code space on your target platform, you might want to do something else. Commented Oct 13, 2016 at 11:49

1 Answer 1

0

You would want PI to be constant and be in the scope for as much the app runs so static const PI would be good

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.