Thanks a lot.
I understand one shouldn't use #define for simple comstants as Elysia suggests. Anyway, won't it be a good idea to place the "#define pi 3.142" outside the main()?
Best wishes
Jackson
Thanks a lot.
I understand one shouldn't use #define for simple comstants as Elysia suggests. Anyway, won't it be a good idea to place the "#define pi 3.142" outside the main()?
Best wishes
Jackson
I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.
Thank you, Elysia.
So, a macro (or, pre-processor directive) would be accessible from anywhere in the code. But still I have to define (or, write it) before using it. Correct?
I'm not an English speaker as you know, so would you please tell me what you mean by "as opposed to using a constant"?
Thanks a lot.
I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.
Yeah, you got it right.
By "as opposed to using a constant" I meant that you will get benefits from carefully choosing a scope when using a constant (that is, a variable declared as const), but you don't get any benefits from carefully choosing scope with macros (the scope is ignored).
Thank you, Elysia.
Is there a particular term to refer to the feature of C++ which requires one to define something before using it? Please let me know. Thanks.
Regards
Jackson
I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.
Well C++ is a typesafe language, meaning that it prevents using type A in place of type B when type B is required and that's pretty much why variables have to be defined before they can be used.
I think I need to rephrase my question.
Is there a particular term to refer to the feature of C++ which requires one to define something before using it? For example, one cannot use a variable unless it has been defined earlier.
I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.
declaring a variable...
@jackson6612 ... Have they gotten you adequately confused yet?
Preprocessor statements such as #define, #include, #ifdef, etc. are not source code and probably should not be included in source code... What they do is give the compiler instructions on how to see your code while compiling it. I always make it a rule that *whenever possible* the preprocessor stuff should be at the top of the page, outside of any code... So...
Code://define_pi.cpp // learning to use #define on Pi #include <iostream> #include <cstdlib> #include <cmath> #define pi 3.142 using namespace std; int main() { float r, area; cout << "enter radius"; cin >> r; area = 2*pi*r; cout << "area is: " << area << endl; system("pause"); return 0; }
Thank you.
I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.